how to parse string into json object javascript [duplicate] - javascript

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.

Related

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 escape double and single quote from JS string [duplicate]

This question already has answers here:
Javascript, Razor and Escape characters. Like apostrophe
(5 answers)
Closed 7 years ago.
I'm assigning the value of JS string variable from server side code.
Like this
var fbShareTitle = "#(ViewBag.LeadTitle as string)";
ViewBag return string value is
A "Fantastic" Lead With 'Qoute'
Now It is giving error in console
SyntaxError: missing ; before statement
I have tried this
var fbShareTitle = ("#(ViewBag.LeadTitle)").replace(/"/g, '\\"');
But now I'm getting this error.
SyntaxError: missing ; before statement
As This string will be shared on fb, So i can't modify string, like replace all " with ' e.t.c.
The reason why your code doesn't work is that Razor will generate the following:
var fbShareTitle = "A "Fantastic" Lead With 'Qoute'";
which is invalid JavaScript. You can't simply fix it by replace, since it's not the problem that your string is bad, it's that your code can't parse - replace never gets to execute. You need to fix it on serverside, where you generate the JavaScript in question, by modifying your Razor code:
var fbShareTitle = #Html.Raw(Json.Encode(ViewBag.LeadTitle as string));
Json will take care of quotes and proper escaping; Raw will make sure you don't get your < and > replaced. Extra benefit from #Html.Raw(Json.Encode(...)) mantra: you can use it to inject any kind of data that can be encoded in JSON, not only strings.

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

Reading json object with key starting with '#' character [duplicate]

This question already has answers here:
How to access object property with invalid characters
(2 answers)
Closed 7 years ago.
My JSON structure is as follows
"{"Key":{"#text":"100150410150347261963/output/Five String.mp4"},"LastModified":{"#text":"2015-05-26T15:33:39.000Z"},"ETag":{"#text":"\"5e5fd36802186f81109a9adedcb802fe\""},"Size":{"#text":"18831126"},"StorageClass":{"#text":"STANDARD"}}"
This is my code
var data = JSON.parse("{"Key":{"#text":"100150410150347261963/output/Five String.mp4"},"LastModified":{"#text":"2015-05-26T15:33:39.000Z"},"ETag":{"#text":"\"5e5fd36802186f81109a9adedcb802fe\""},"Size":{"#text":"18831126"},"StorageClass":{"#text":"STANDARD"}}");
var key = data.Key;
Now I want to read the value '100150410150347261963/output/Five String.mp4' but the key to this value is '#text', which starts with a # character. How can I read this?
var value = key.#text;
or
var value = key.'#text';
is not working. Is there any way to read this value?
PS: Please ignore the escaping of double quote '"' characters
Use bracket notation:
var value = data.Key['#text'];

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

Categories