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\"] }"