Javascript Objects are NOT JSON

The headline should more accurately read “Javascript Objects are not necessarily JSON.” But that waters it down a bit, don’t you think?
I know this has been posted a few times before, but this is not something I knew until recently, and after learn…


This content originally appeared on Zach Leatherman and was authored by Zach Leatherman

The headline should more accurately read “Javascript Objects are not necessarily JSON.” But that waters it down a bit, don’t you think?

I know this has been posted a few times before, but this is not something I knew until recently, and after learning it, have noticed quite a few other people doing it wrong as well. So, to risk adding another reverb to the echo chamber, I’ll post a link to a good explanation by Jesse Skinner on the specifics of the JSON specification.

The main point here is that all object keys and strings in JSON must have double quotes. I encourage you to look through the train track specification to verify for yourself.

If you try to use Captain Crockford’s JSON Parser, it won’t parse your string with single quotes or (un/single)-quoted object keys. His parser has a nice regex checker built-in (and a walker callback to let you parse date strings into Date() objects). It’s a good way to verify that external data, or a data source not under your control, is safe to eval and use in your own code.

Here are a few quick examples:

Valid JSON #

{"myKey": "myString"}

Don’t use single quotes. Don’t use unquoted object keys.

Invalid JSON #

{myKey: "myString"}
{'myKey': 'myString'}

Credits to Jonathan Snook for the initial write-up and Crockford for formalizing JSON.


This content originally appeared on Zach Leatherman and was authored by Zach Leatherman


Print Share Comment Cite Upload Translate Updates
APA

Zach Leatherman | Sciencx (2008-05-09T05:00:00+00:00) Javascript Objects are NOT JSON. Retrieved from https://www.scien.cx/2008/05/09/javascript-objects-are-not-json/

MLA
" » Javascript Objects are NOT JSON." Zach Leatherman | Sciencx - Friday May 9, 2008, https://www.scien.cx/2008/05/09/javascript-objects-are-not-json/
HARVARD
Zach Leatherman | Sciencx Friday May 9, 2008 » Javascript Objects are NOT JSON., viewed ,<https://www.scien.cx/2008/05/09/javascript-objects-are-not-json/>
VANCOUVER
Zach Leatherman | Sciencx - » Javascript Objects are NOT JSON. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2008/05/09/javascript-objects-are-not-json/
CHICAGO
" » Javascript Objects are NOT JSON." Zach Leatherman | Sciencx - Accessed . https://www.scien.cx/2008/05/09/javascript-objects-are-not-json/
IEEE
" » Javascript Objects are NOT JSON." Zach Leatherman | Sciencx [Online]. Available: https://www.scien.cx/2008/05/09/javascript-objects-are-not-json/. [Accessed: ]
rf:citation
» Javascript Objects are NOT JSON | Zach Leatherman | Sciencx | https://www.scien.cx/2008/05/09/javascript-objects-are-not-json/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.