Single quotes within json value [duplicate] - javascript

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);

Related

How to backslash in number using javascript [duplicate]

This question already has answers here:
How do I put a single backslash into an ES6 template literal's output?
(2 answers)
Closed 4 months ago.
How to add backslash in a number dynamically in JavaScript.
I want output like this : '/(123) 456/-7890'
let number = '1234567890';
let test = `\(${number.substr(0,3)}) ${number.substr(3,3)}'\'-${number.substr(6,4)}`;
Backslash removed after getting the output '(123) 456-7890'
You need to double the \ because it is a special escape character.
let number = '1234567890';
let test = `\\(${number.substr(0,3)}) ${number.substr(3,3)}'\\'-${number.substr(6,4)}`;

How to convert enclosed double quotes to single quotes in javascript? [duplicate]

This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Closed 3 years ago.
I am experiencing a very strange behavior of Javascript.
I get the data object in the form of a string from the server as shown below,
"{'id':1234, 'name'}"
When I try to parse this data using JSON.parse() it throws
JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data
However changing the data to '{"id":1234, "name"}' will work.
But my question is how do I transform:
"{'id':1234, 'name'}" to '{"id":1234, "name"}'
in the javascript end? (I dont want to change any thing in the server).
Simply you need to replace the character(') as global, here the code:
var yourString = "{'id':1234, 'name'}";
yourString = yourString.replace(/\'/g, '"');
console.log(yourString);
And in vice versa:
var yourString = '"{\'id\':1234, \'name\'}"';
yourString = yourString.replace(/\'/g, '"');
yourString = yourString.replace(/\"/g, "'");
console.log(yourString);
Here other example with mixed characters( " and ' ):
var yourString = "{\"id':1234, 'name'}";
yourString = yourString.replace(/\'/g, '"');
console.log(yourString); // Automatically skips the right character(")

how to parse string into json object javascript [duplicate]

This question already has answers here:
Converting a string to JSON object
(10 answers)
Closed 6 years ago.
I'm trying to parse this string into a JSON:
"{'firstname':'Jesper','surname':'Aaberg','phone':'555-0100'}"
I'm doing it like so:
var strJSON = "{'firstname':'Jesper','surname':'Aaberg','phone':'555-0100'}";
console.log(JSON.parse(strJSON));
But I get the error message:
VM652:1 Uncaught SyntaxError: Unexpected token ' in JSON at position 1
at JSON.parse ()
Does anybody know what am I missing and how can i solve it?
You can replace single quotes to double quotes and parse it.
var str = "{'firstname':'Jesper','surname':'Aaberg','phone':'555-0100'}";
var o = JSON.parse(str.replace(/\'/g, "\""));
console.log(o)
Single quotes are not valid for strings, you need to use double quotes instead:
var strJSON = '{"firstname":"Jesper","surname":"Aaberg","phone":"555-0100"}';
Just change your string:
"{'firstname':'Jesper','surname':'Aaberg','phone':'555-0100'}"
to:
'{"firstname":"Jesper","surname":"Aaberg","phone":"555-0100"}'
JSON only supports double-quotes
var str = '{"firstname":"Jesper","surname":"Aaberg","phone":"555-0100"}';
console.log(JSON.parse(str));
Use this.

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

How to include '\' character in a javascript string [duplicate]

This question already has answers here:
Escaping backslash in string - javascript
(5 answers)
Closed 8 years ago.
I'm trying to create a string in javascript which needs to be valid JSON the string is
{"query":"FOR u IN Countries RETURN {\"_key\":u._key}"}
I keep going around in circles as to how i can include the \ character given its also used to escape the quote character. can anyone help ?
Escape the backslash itself:
{"query":"FOR u IN Countries RETURN {\\\"_key\\\":u._key}"}
First pair of backslashes represents '\' symbol in the resulting string, and \" sequence represents a double quotation mark ('"').
Let JSON encoder do the job:
> s = 'FOR u IN Countries RETURN {"_key":u._key}'
"FOR u IN Countries RETURN {"_key":u._key}"
> JSON.stringify({query:s})
"{"query":"FOR u IN Countries RETURN {\"_key\":u._key}"}"
Use \\ for double quoted strings
i.e.
var s = "{\"query\":\"FOR u IN Countries RETURN {\\\"_key\\\":u._key}\"}";
Or use single quotes
var s = '{"query":"FOR u IN Countries RETURN {\"_key\":u._key}"}';

Categories