JSON.parse not working for valid json [duplicate] - javascript

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":""}');

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.

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

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 (\").

Single quotes within json value [duplicate]

This question already has answers here:
How can I accommodate a string with both single and double quotes inside of it in JavaScript
(4 answers)
How to escape special characters in building a JSON string?
(10 answers)
Closed 7 years ago.
Javascript fails to read this json string as it contains a single quote character which it sees as the end of the string.
How can I escape the single quote so that it is not seen as the end of the string?
var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It's a test!"}}';
var parsed = JSON.parse(json);
Use a backslash to escape the character:
var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';
var parsed = JSON.parse(json);
Just escape the single quote with a backslash such as \':
var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';
var parsed = JSON.parse(json);
//Output parsed to the document using JSON.stringify so it's human-readable and not just "[object Object]":
document.write(JSON.stringify(parsed));
Escape it with a backslash
var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';
var parsed = JSON.parse(json);

javascript string split for string containing backslashes [duplicate]

This question already has answers here:
Need a basename function in Javascript
(19 answers)
Closed 8 years ago.
I have a string like C:\Users\shail.jet\Desktop\cca-lan_test_cases_path.txt.
I want to get only file name that is in this case is cca-lan_test_cases_path.txt.I have tried it with javascript split function but it did'nt work. Any help will be appreciated.
"C:\\Users\\shail.jet\\Desktop\\cca-lan_test_cases_path.txt".split("\\").pop();
Make sure you add escape backslashes in the file path string otherwise javascript will ignore it.
That will split by backslash and then use pop to get the last element in the array which will be the file name.
var filename = fullPath.replace(/^.*[\\\/]/, '')
This will handle both \ OR / in paths
var filename = fullPath.replace(/^.*(\\|\/|\:)/, '');
should also protect from empty string

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