JavaScript runtime error: Invalid character using JSON.parse [duplicate] - javascript

This question already has answers here:
jQuery.parseJSON single quote vs double quote
(4 answers)
Closed 7 years ago.
The following throws "JavaScript runtime error: Invalid character":
var opts = "[{'UOM':'E','Description':'E - Each'},{'UOM':'C','Description':'C - Per 100'},{'UOM':'M','Description':'M - Per 1000'}]"
var options = JSON.parse(opts);
What invalid character is in my JSON string? Thanks

You need to replace the apostrophes with escaped quotation marks (\").

Related

Why JSON.parse for one backslash returns two backslashes, but not four ones [duplicate]

This question already has answers here:
JavaScript: A BackSlash as part of the string
(3 answers)
How to deal with backslashes in json strings php
(6 answers)
Closed 3 years ago.
I have the string with backslashes:
const str = 'E:\\music\\post'; // It represents "E:\music\post" text in HTML
JSON.stringify(str) returns "the same" string '"E:\\music\\post"'. Why not '"E:\\\\music\\\\post"'?
JSON.parse('"E:\\music\\post"') will throws the error: Uncaught SyntaxError: Unexpected token m in JSON at position 4.
JSON.parse('"E:\\\\music\\\\post"') works OK.
But another strange thing is that JSON.parse(JSON.stringify("E:\\music\\post")) works well. Why?
That is the better workaround for it? I want to save the string to json file, and later to create an object from the json file.

Set value with backslash to input in JQuery [duplicate]

This question already has answers here:
Escaping backslash in string - javascript
(5 answers)
JavaScript backslash (\) in variables is causing an error
(5 answers)
Closed 4 years ago.
I have some input
<input id = 'some_id' type='text'/>
I want to set value with backslash, for example:
$('#some_id').val('someval \')
and I'm getting error:
Uncaught SyntaxError: Invalid or unexpected token
How can I show string value with backslash in input ?
Escape the \ character like this: $('#some_id').val('someval \\')
$('#some_id').val('someval \\')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id = 'some_id' type='text'/>

Javascript Regex not working with Node [duplicate]

This question already has answers here:
Regex works on browser but not in Node.js
(2 answers)
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 4 years ago.
I have the following regex that works when used on the chrome browser:
file.match(/(?<=__\()(.*)(?=\))/g);
But it seems to throw an error when using Node on my machine:
SyntaxError: Invalid regular expression: /(?<=__\()(.*)(?=\))/: Invalid group
Any ideas why this might be? Also I am trying to match string that appear in groups like this:
__("string to match");
const file = "__('string to be extracted');";
const strings = file.match(/(?<=__\()(.*)(?=\))/g);
console.log(strings);

JSON.parse not working for valid json [duplicate]

This question already has answers here:
json parse error with double quotes
(9 answers)
Closed 8 years ago.
I am using the below line in Javascript to parse a json string.
var obj = JSON.parse('{"respDataMap":{"userMessages":{"lbSearchHint":"Enteravalueandpress\"Enter\"orclickon\"Search\"#"}},"respErrorCode":"","respErrorMessage":""}');
The escaped double-quote character in the string is causing the json parsing to fail.
But, the same string pasted in online JSON validators is certified as valid.
How do I fix this?
Use double slash like for escape character
var obj = JSON.parse('{"respDataMap":{"userMessages":{"lbSearchHint":"Enteravalueandpress\\"Enter\\"orclickon\\"Search\\"#"}},"respErrorCode":"","respErrorMessage":""}');

Parsing JSON containing new line characters [duplicate]

This question already has answers here:
How do I handle newlines in JSON?
(10 answers)
Closed 8 years ago.
In my website I try to convert a string to JSON which contains a newline.
JSON.parse('{"hallo":"line1\r\nline2","a":[5.5,5.6,5.7]}');
This produces an "Unexpected token" error. Do I need to escape that somehow?
Yes, you should escape both \n and \r as they belong to the list of control characters. Full list of characters that need to be escaped can be found here. Your code would be
obj = JSON.parse('{"hallo":"line1\\r\\nline2","a":[5.5,5.6,5.7]}');
JSFiddle: link
Try:
JSON.parse('{"hallo":"line1\\r\\nline2","a":[5.5,5.6,5.7]}');

Categories