Skip to content
Tony Pujals edited this page Jul 7, 2015 · 1 revision

What is JSON?

JSON is a lightweight, text-based, language-independent, data exchange format for the portable representation of structured data. There is an official ECMA specification that describes it in precise detail, but essentially JSON format is easy for machines to generate and parse, while having the virtue of also being relatively easy for humans to read and write.

Using JSON, programmers can describe data structures comprised of (potentially nested) collections of unordered name/value pairs and ordered lists of values, as the following example demonstrates:

{
    "id": 1,
    "name": "A green door",
    "price": {
      "amount": 12.50,
      "currency": "USD"
    },
    "tags": ["home", "green"]
}

The structure starts with the declaration of an object (contained within the outermost (first and last) left { and right } brace pairs containing a number of members. Each member is comprised of a name/value pair, where the name is a string and the value can be any of the following:

  • string
  • number
  • object
  • array
  • true or false
  • null

Note that while a JSON document contains data represented in a particular data structure, JSON is only concerned with governing how that structure is formatted, not with enforcing any particular structure. The same data could be expressed in valid JSON as follows:

{
    "product-id": "1",
    "name": "A green door",
    "cost": "$12.50",
    "description": {
      "type": "residential",
      "color": "green"
    }
}

Expressing structure and type information

As humans, either representation makes sense to us, and since both are formatted properly according to the rules of JSON, either one can be successfully parsed by a machine as well. As long as both sender and receiver agree on the actual structure of the data along with the types of values represented, either one can be successfully processed.

How do the sender and receiver coordinate on the schema, which is metadata about the structure of fields of data as well as valid values for those fields, of the JSON data?

One way is to provide official documentation and make it available to programmers. Here are two examples:

  • Twitter's documentation on the format of Tweets
  • Facebook's documentation on the format of a Post

By studying the documentation, programmers can use the information to write code that will be able to successfully exchange data with Twitter or Facebook. This code can even be shared in libraries that ensure schema rules are respected when generating or processing JSON instances.

However, it seems that there should be something better than an ad hoc approach to schema representation. A formal, standardized approach is desirable not only for eliminating potential ambiguity about what it means to be a valid schema, but because it allows a schema definition to be fed into validators that can automate the validation of JSON data.

After all, we rely on the JSON standard for how we format structured data; we don't document ad hoc data formats, such as using single quotes instead of double quotes for string delimiters, and expect receivers to have to write custom generators and parsers to be able to exchange data.

We should also be able to rely on a standard for how we represent schema information for instances of JSON structured data, and, as we'll see in the next section, that is exactly the intended purpose of JSON Schema.

For more information, see [JSON Schema Overview](JSON Schema Overview).

Clone this wiki locally