Convert object string to JSON
If the string is from a trusted source, you could use eval
then JSON.stringify
the result. Like this:
var str = "{ hello: 'world', places: ['Africa', 'America', 'Asia', 'Australia'] }";
var json = JSON.stringify(eval("(" + str + ")"));
Note that when you eval
an object literal, it has to be wrapped in parentheses, otherwise the braces are parsed as a block instead of an object.
I also agree with the comments under the question that it would be much better to just encode the object in valid JSON to begin with and avoid having to parse, encode, then presumably parse it again. HTML supports single-quoted attributes (just be sure to HTML-encode any single quotes inside strings).
Your string is not valid JSON, so JSON.parse
(or jQuery's $.parseJSON
) won't work.
One way would be to use eval
to "parse" the "invalid" JSON, and then stringify
it to "convert" it to valid JSON.
var str = "{ hello: 'world', places: ['Africa', 'America', 'Asia', 'Australia'] }"
str = JSON.stringify(eval('('+str+')'));
I suggest instead of trying to "fix" your invalid JSON, you start with valid JSON in the first place. How is str
being generated, it should be fixed there, before it's generated, not after.
EDIT: You said (in the comments) this string is stored in a data attribute:
<div data-object="{hello:'world'}"></div>
I suggest you fix it here, so it can just be JSON.parse
d. First, both they keys and values need to be quoted in double quotes. It should look like (single quoted attributes in HTML are valid):
<div data-object='{"hello":"world"}'></div>
Now, you can just use JSON.parse
(or jQuery's $.parseJSON
).
var str = '{"hello":"world"}';
var obj = JSON.parse(str);