Expanding Your Toolkit: What is JSON?

Expanding Your Toolkit is an occasional column explaining technology which can extend the reach of your system.

JSON (JavaScript Object Notation) — The name tells us that this is a way of notating the contents of a JavaScript object. As we will see, JSON is neither JavaScript-specific nor a full-featured object notation. So what's with the name, and more importantly, what is it really?

JSON was invented for use by a small group of JavaScript programmers who were much smarter about technology than they were about naming things.

By design, JSON is literally XML Light. Among other things, XML is the X in AJAX (Asynchronous JavaScript And XML). Douglas Crockford et al. wanted to use something simpler and smaller in the AJAX context. JSON is the result. You might say it's an application of the 80-20 Rule (or maybe the 99-1 Rule). JSON is not as feature-rich as XML, but it adequately supports a huge range of common use-cases more elegantly. Indeed, it may even be a good idea to force more sophisticated datasets into JSON's world, simplifying interfaces while lessening training and support costs for a service that then only requires its clients to handle JSON instead of XML. At the least, it cuts down on the complexity and size of the payload being produced, sent over the internet, and consumed — Crockford's goal.

It Supports Less Than JavaScript

If a JavaScript object has methods (which is very common in practice), they silently just do not show up when that object is expressed in JSON (via the definitive generator JSON.stringify() ). This seems to reflect the fact that the use-cases Crockford et al. had in mind did not include transferring code between browsers and back ends, so it was a feature to ignore methods, automatically restricting payloads to items of value. Minimizing payload was a key objective.

There are other data types (maps, sets, and more) and special values ( Infinity , NaN , and undefined ) in JavaScript that also silently fail to be represented in JSON. You can, of course, create your own non-standardized, non-automatic, application-specific (or library-specific) encoding and decoding for anything , but that is something you may do with JSON, not something that JSON represents or that its standard generators and parsers do for you.

Perhaps this is because the backends, with which browsers communicate, are rarely implemented in JavaScript, and universal, automatic cross-language translations of uncommon items is a creature best left unprovoked. One can argue that if you need these things, then it should be application level code that deals with them in each language's own idioms.

It Supports More Than JavaScript

Oddly, JSON allows some characters in strings that are not allowed in JavaScript.

More importantly, JSON is not in any functional way linked to JavaScript — it's just badly named. JSON has proved so useful and widely used that there are JSON generator-parser pairs available for most web-oriented languages, including Perl, PHP, Python, Ruby, Java, ASP, C#, etc. Since the syntax is simple and the number of supported data types is small, creating a generator-parser pair is a modest programming task in any language for which a set does not already exist. (See this article on the International Spectrum website, for example.)

In practice, JSON is a language-independent data container used for more browser back-end and web service traffic than any other and is also used for configuration files and other storage purposes.

The Data

JSON allows for six kinds of data to be expressed. Numbers are integers and floats (with no distinction between them). Strings are runs of zero or more Unicode characters inside quotation marks. Booleans are true or false . Null is a data type with a single possible value of null . Arrays are collections of arbitrary elements, such as [ "two" , 1 , { "pi" : 3.14159 } ]. Objects are associative arrays, each element being an NVP (Name-Value Pair). For example { "dbType" : "Pick multi-value", "born" : 1965 } . As shown in these examples, array and object elements do not need to be of like type.

The Syntax

Strings are delimited with quotation marks (and not by apostrophes or so-called single quotes). Strings can contain problematic characters via escape sequences begun with a backslash ( \ ). An array is indicated by square brackets ( [ ] ). An object is indicated by curly braces ( { } ). A colon ( : ) is placed between a name and the its value. Names themselves are strings, not unquoted tokens. A comma ( , ) is used to separate adjacent array elements or object properties. Empty arrays and objects are allowed, as is nesting to any depth. Whitespace outside of quotation marks can and should be used to maximize human readability.

There is something to be said for a structured data format that can be completely defined in two paragraphs and serves so many actual use-cases that it dominates data exchange activity on the web!


Let's look at a simple example [ Figure 1 ]. This object holds information about a well-known duck.

    "first name" : "Donald",
    "last name"  : "Duck",
    "address" : {
        "home" : {
            "line1" : "123 Maple Street",
          "line2" : null,
          "state" : "CA",
          "city"  : "Los Angeles"
        "work" : {
          "line1" : "Disneyland Resort",
          "line2" : "1313 S Harbor Blvd",
          "state" : "CA",
          "city"  : "Anaheim"
    "anthropomorphic" : true,
    "birthdate" : { "month" : "June", "day" : 9, "year" : 1934 },
    "military status" : [ { "army" : "retired" }, { "navy" : "reservist" } ],
    "family" : { "nephew" : [ "Huey", "Dewey", "Louie" ] }

Fig. 1


When used for configuration files, the fact that JSON does not support comments is a drawback. One typically wants to annotate a config file with the reason each value there was chosen, and what factors should govern setting a different value. You can easily implement a file reader that strips comments and then feeds what is left into a JSON parser. Consider the following illustration of an obvious possibility that is trivial to implement [ Figure 2 ].

"version" : 1.7,
"timezone" : "America/Los_Angeles", // used in error logging --
// set to match server's time zone
"resource" : "http://coolplace.com/resource", // the only tricky bit to parse

Help is on Hand

As simple as JSON is, when you are new to it, using an online resource such as jsonlint.com will tell you when you have it right, or help you figure out where you went wrong.

Bennett Barouch

Bennett Barouch, an executive at eBay, a Fortune 500 company, has been VP of Engineering at a half-dozen startup companies. Work he led is in the permanent collection of the Smithsonian, for Outstanding Achievement in Information Technology. incredible combination of experience.

View more articles


Mar/Apr 2017