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
More recent articles
- Qwen2.5-Coder-32B is an LLM that can code well that runs on my Mac - 12th November 2024
- Visualizing local election results with Datasette, Observable and MapLibre GL - 9th November 2024
- Project: VERDAD - tracking misinformation in radio broadcasts using Gemini 1.5 - 7th November 2024