JSON.parse not working with escaped quotes [duplicate] - javascript

This question already has answers here:
JavaScript - Escape double quotes
(2 answers)
Closed 3 years ago.
The following code is throwing an error:
alert(JSON.parse('{"name":"Quick Write \"English\"","category":"qwer"}'));
/*
{
"name": "Quick Write \"English\"",
"category":"qwer"
}
*/
It says
Uncaught SyntaxError: Unexpected token E in JSON at position 23
How do I make escaped quotes work?

I believe you can use double \ instead of \
let x = '{"name":"Quick Write \\"English\\"","category":"qwer"}';
console.log(JSON.parse(x));

Related

Javascript - Uncaught Error: Syntax error, unrecognized expression [duplicate]

This question already has answers here:
What are valid values for the id attribute in HTML?
(26 answers)
Need to escape a special character in a jQuery selector string
(7 answers)
Closed 2 years ago.
Edit:
I think the issue is not with concatenation itself but the special character in the id of the element I am trying to select.
Original:
I am having problem with concatenating two strings. It seems the problem is because one of the strings contains '!'. Sadly, I have no control over what that string can contain. I tried using '+' to make single string of the two before I tried concat(). But the error remains the same.
The relevant code is $('#id_online_status_'.concat(msg.id)).removeClass('text-success').addClass('text-muted');
Error is this:
Uncaught Error: Syntax error, unrecognized expression: #id_online_status_specific.YqzvRnpU!OPMxkuoFQILY
Please help with fixing this. I am not much familiar with JS in general.
change your code like this and try
$('#id_online_status_'+msg.id).removeClass('text-success').addClass('text-muted');
remove 'concat' and try with (+)

Set \ as string in JavaScript [duplicate]

This question already has answers here:
JavaScript backslash (\) in variables is causing an error
(5 answers)
Closed 7 years ago.
How do I set a \ as a string in JavaScript I am using it like this
Var x = {"\":"some value"}
But it's says that that is illegal, so then I tried it like '\' this but still the same error happened.
You can use escaping characters so add one more \ it will be escaped and become a string like this var x = {"\\":"some value"}
Just add an other slash '\' before it.

Javascript eval fails on complicated json array [duplicate]

This question already has answers here:
JSON Javascript escape
(4 answers)
Closed 7 years ago.
I want to convert a json string to object by eval, but it fails with error like:
Uncaught SyntaxError: Unexpected identifier VM250:1
below is my string:
'[{"quiz_id":"3","_id":"1","option_in_json":"[{\"option\":\"1\",\"is_answer\":false},{\"option\":\"2\",\"is_answer\":true}]","question":"1+1"}]';
Seems there is something wrong in the bold part, but i don't know how to fix it
The code below is not working
var m='[{"quiz_id":"3","_id":"1","option_in_json":"[{\"option\":\"1\",\"is_answer\":false},{\"option\":\"2\",\"is_answer\":true}]","question":"1+1"}]';
eval(m);
The code below is working so i think the data structure of this json string is ok
var m=[{"quiz_id":"3","_id":"1","option_in_json":"[{\"option\":\"1\",\"is_answer\":false},{\"option\":\"2\",\"is_answer\":true}]","question":"1+1"}];
alert(m[0].option_in_json);
Also tried with $.parseJSON with no luck
It does not work because you are not escaping the data inside the string literal correctly. Look at the value of m in the first case, especially the quotation marks:
[{"option_in_json":"[{"option":"1","is_answer":false}]","question":"1+1"}]
// ^ ^
I removed some irrelevant data. You should be able to see that this cannot be valid JavaScript (or JSON), because the quotation mark before option terminates the string.
In order to put the data inside a string literal, you should either fix the data so that it doesn't contain nested JSON, or escape \:
'[{"option_in_json":"[{\\"option\\": ... }]"}]'
Better of course if you are not putting it in a string literal in the first place.
var m='[{"quiz_id":"3","_id":"1","option_in_json": [{"option":"1","is_answer":false},{"option":"2","is_answer":true}],"question":"1+1"}]';
// ^-- don't wrap in "" so no need to escape inner double quotes.
console.dir(JSON.parse(m));

escaping $ sign for String.replace [duplicate]

This question already has answers here:
`string.replace` weird behavior when using dollar sign ($) as replacement
(3 answers)
Closed 8 years ago.
At following String.replace line, lossLocation.state$ ,
the $ sign is removed after the replace,
i need to keep the $, as it is used in the variable.
'{0}'.replace(
'{0}'
, 'categories.Country === \'$formData.lossLocation.state$\'.toUpperCase()')
It gives me
"categories.Country === '$formData.lossLocation.state'.toUpperCase()"
The expected outcome should be
"categories.Country === '$formData.lossLocation.state$'.toUpperCase()"
i've tried the following but still been removed when replace
state\$
As it states in String.prototype.replace(). To escape '$' in replacement content, you should use '$$' instead of '\$'.
So a proper way of constructing it would be
'{0}'.replace('{0}',
'categories.Country === \'$formData.lossLocation.state$\'.toUpperCase()'
.replace(/\$/g, '$$$$')
)

CGI.unescape and CGI.unescapeHTML don't do anything [duplicate]

This question already has answers here:
raw vs. html_safe vs. h to unescape html
(7 answers)
Closed 8 years ago.
I have a string that I receive as a response to a get request
{"revision"=>"r2407", "full_version"=>"2.5 [r2407]", "full_name"=>" [r2407]", "version"=>"2.5"}
In a js file:
$('.output').text('<%=CGI.unescape(#response.to_s)%>')
I still get the same string with &quot's and stuff. I also tried JS unescape() and it didn't do anything too. What's wrong?
CGI.escape and CGI.unescape is to handle URL-encoded string.
CGI.escape('&')
# => "%26"
CGI.unescape('%26')
# => "&"
Use CGI::unescape_html instead:
CGI.unescape_html('>')
# => ">"
body = '{"revision"=>"r2407", "full_version"=>"2.5 [r2407]", "full_name"=>" [r2407]", "version"=>"2.5"}'
CGI.unescape_html(body)
# => {"revision"=>"r2407", "full_version"=>"2.5 [r2407]", "full_name"=>" [r2407]", "version"=>"2.5"}
I think you first need to unescape the HTML from response using CGI.unescape_html, then escape it for JavaScript with j:
$('.output').text('<%=j CGI.unescape_html(#response.to_s)%>')

Categories

Resources