JSON: not just an object literal

For a while I’ve considered JSON to be the same as an object literal in JavaScript.

How wrong I’ve been.

JSON doesn’t support half of this stuff. The JSON format has been developed specifically for data exchange and supports only a subset of what an object literal does.

First of all, property names must be qualified by a double-quote. Single-quotes are not valid. That one had me confused for while as I tried to validate my code.

Secondly, values can only be one of the following: null, string, number, boolean, array, or another JSON object. Functions are definitely not gonna fly.

Generally you’ll be using some other library to generate JSON based on your objects (that’s what kept me so unelightened for so long).

However, for when you need to cut it by hand, there’s a nice little tool at jsonlint.com which you can use to validate your objects.

Tagged ,

One thought on “JSON: not just an object literal

  1. Mick says:

    Newtonsoft has a great opensource library for serialising/deserialising, and creating JSON objects.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: