Simon Willison’s Weblog

Subscribe

How can I parse unquoted JSON with JavaScript?

4th February 2012

My answer to How can I parse unquoted JSON with JavaScript? on Quora

Unquoted JSON isn’t JSON—the JSON spec requires that strings are quoted (with double quotes, not single quotes).

If you have JSON with unquoted strings what you actually have is just plain JavaScript. So run eval() on it:

var obj = eval(’(’ + invalid_json + ’)’);

(adding the parentheses to the string ensures object literal syntax will be correctly handled).

Update: as pointed out in a comment, you should NOT do this if the JSON is from an untrusted source, especially if you are running the code in Node.js

This is How can I parse unquoted JSON with JavaScript? by Simon Willison, posted on 4th February 2012.

Next: If python dictionaries are inherently orderless, why were they given the name if a real dictionary is sorted by letter?

Previous: Was CoffeeScript invented to help Ruby programmers get over that dirty yucky feeling they get when working in JavaScript?

Monthly briefing

Sponsor me for $10/month and get a curated email digest of the month's most important LLM developments.

Pay me to send you less!

Sponsor & subscribe