How can I convert a string that describes an object into a JSON string using JavaScript (or jQuery)?
e.g: Convert this (NOT a valid JSON string):
var str = "{ hello: 'world', places: ['Africa', 'America', 'Asia', 'Australia'] }"
into this:
str = '{ "hello": "world", "places": ["Africa", "America", "Asia", "Australia"] }'
I would love to avoid using eval()
if possible.
This question is related to
javascript
json
object
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);
Just for the quirks of it, you can convert your string via babel-standalone
var str = "{ hello: 'world', places: ['Africa', 'America', 'Asia', 'Australia'] }";_x000D_
_x000D_
function toJSON() {_x000D_
return {_x000D_
visitor: {_x000D_
Identifier(path) {_x000D_
path.node.name = '"' + path.node.name + '"'_x000D_
},_x000D_
StringLiteral(path) {_x000D_
delete path.node.extra_x000D_
}_x000D_
}_x000D_
}_x000D_
}_x000D_
Babel.registerPlugin('toJSON', toJSON);_x000D_
var parsed = Babel.transform('(' + str + ')', {_x000D_
plugins: ['toJSON']_x000D_
});_x000D_
var json = parsed.code.slice(1, -2)_x000D_
console.log(JSON.parse(json))
_x000D_
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
_x000D_
A solution with one regex and not using eval:
str.replace(/([\s\S]*?)(')(.+?)(')([\s\S]*?)/g, "$1\"$3\"$5")
This I believe should work for multiple lines and all possible occurrences (/g flag) of single-quote 'string' replaced with double-quote "string".
var str = "{ hello: 'world', places: ['Africa', 'America', 'Asia', 'Australia'] }";
var json = JSON.stringify(eval("(" + str + ")"));
There's a much simpler way to accomplish this feat, just hijack the onclick attribute of a dummy element to force a return of your string as a JavaScript object:
var jsonify = (function(div){
return function(json){
div.setAttribute('onclick', 'this.__json__ = ' + json);
div.click();
return div.__json__;
}
})(document.createElement('div'));
// Let's say you had a string like '{ one: 1 }' (malformed, a key without quotes)
// jsonify('{ one: 1 }') will output a good ol' JS object ;)
Here's a demo: http://codepen.io/csuwldcat/pen/dfzsu (open your console)
Use with caution (because of eval()
):
function strToJson(str) {
eval("var x = " + str + ";");
return JSON.stringify(x);
}
call as:
var str = "{ hello: 'world', places: ['Africa', 'America', 'Asia', 'Australia'] }";
alert( strToJson(str) );
I hope this little function converts invalid JSON string to valid one.
function JSONize(str) {
return str
// wrap keys without quote with valid double quote
.replace(/([\$\w]+)\s*:/g, function(_, $1){return '"'+$1+'":'})
// replacing single quote wrapped ones to double quote
.replace(/'([^']+)'/g, function(_, $1){return '"'+$1+'"'})
}
Result
let invalidJSON = "{ hello: 'world',foo:1, bar : '2', foo1: 1, _bar : 2, $2: 3, 'xxx': 5, \"fuz\": 4, places: ['Africa', 'America', 'Asia', 'Australia'] }"
JSON.parse(invalidJSON)
//Result: Uncaught SyntaxError: Unexpected token h VM1058:2
JSON.parse(JSONize(invalidJSON))
//Result: Object {hello: "world", foo: 1, bar: "2", foo1: 1, _bar: 2…}
I put my answer for someone who are interested in this old thread.
I created the HTML5 data-* parser for jQuery plugin and demo which convert a malformed JSON string into a JavaScript object without using eval()
.
It can pass the HTML5 data-* attributes bellow:
<div data-object='{"hello":"world"}'></div>
<div data-object="{hello:'world'}"></div>
<div data-object="hello:world"></div>
into the object:
{
hello: "world"
}
Disclaimer: don't try this at home, or for anything that requires other devs taking you seriously:
JSON.stringify(eval('(' + str + ')'));
There, I did it.
Try not to do it tho, eval is BAD for you. As told above, use Crockford's JSON shim for older browsers (IE7 and under)
This method requires your string to be valid javascript, which will be converted to a javascript object that can then be serialized to JSON.
edit: fixed as Rocket suggested.
Using new Function() is better than eval, but still should only be used with safe input.
const parseJSON = obj => Function('"use strict";return (' + obj + ')')();
console.log(parseJSON("{a:(4-1), b:function(){}, c:new Date()}"))
// outputs: Object { a: 3, b: b(), c: Date 2019-06-05T09:55:11.777Z }
Maybe you have to try this:
str = jQuery.parseJSON(str)
This will also work
let str = "{ hello: 'world', places: ['Africa', 'America', 'Asia', 'Australia'] }"
let json = JSON.stringify(eval("("+ str +")"));
Your best and safest bet would be JSON5 – JSON for Humans. It is created specifically for that use case.
const result = JSON5.parse("{ hello: 'world', places: ['Africa', 'America', 'Asia', 'Australia'] }");_x000D_
_x000D_
console.log(JSON.stringify(result));
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/json5/0.5.1/json5.min.js"></script>
_x000D_
var str = "{ hello: 'world', places: ['Africa', 'America', 'Asia', 'Australia'] }" var fStr = str .replace(/([A-z]*)(:)/g, '"$1":') .replace(/'/g, "\"")
console.log(JSON.parse(fStr))
Sorry I am on my phone, here is a pic.
Use simple code in the link below :
http://msdn.microsoft.com/es-es/library/ie/cc836466%28v=vs.94%29.aspx
var jsontext = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';
var contact = JSON.parse(jsontext);
and reverse
var str = JSON.stringify(arr);
For your simple example above, you can do this using 2 simple regex replaces:
var str = "{ hello: 'world', places: ['Africa', 'America', 'Asia', 'Australia'] }";
str.replace(/(\w+):/g, '"$1":').replace(/'/g, '"');
=> '{ "hello": "world", "places": ["Africa", "America", "Asia", "Australia"] }'
Big caveat: This naive approach assumes that the object has no strings containing a '
or :
character. For example, I can't think of a good way to convert the following object-string to JSON without using eval
:
"{ hello: 'world', places: [\"America: The Progressive's Nightmare\"] }"
Douglas Crockford has a converter, but I'm not sure it will help with bad JSON to good JSON.
You have to write round brackets, because without them eval
will consider code inside curly brackets as block of commands.
var i = eval("({ hello: 'world', places: ['Africa', 'America', 'Asia', 'Australia'] })");
Source: Stackoverflow.com