JSON Syntax
JSON is simple, and its syntax reflects that. This page explains the JSON syntax.
JSON stores data in objects and arrays. The JSON syntax reflects how these are defined.
JSON Objects
A JSON object is an unordered set of name/value pairs inserted between {}
(curly braces).
In the following example, artistname
is a name and Deep Purple
is its value.
An object can contain zero or more name/value pairs. Multiple name/value pairs are separated by a ,
(comma). So the following are all valid objects:
In JSON, a name is a string. Its value can be an object, array, number, string, true
, false
, or null
.
In the above examples, the values are all strings.
JSON Arrays
A JSON array is an ordered collection of values. It allows you to provide a list of values.
A JSON array begins with [
(left bracket) and ends with ]
(right bracket). Its values are separated by ,
(comma).
Here's an example of an array of artists:
White Space
You can use white space to make your JSON documents more readable. In fact, this is the common convention with JSON and most (if not all) programming languages.
You can use spaces, tabs, carriage returns, line feeds or new line characters to help with the readability of your JSON files.
So we could do this:
Or this:
Those two examples are exactly the same as far as data is concerned. Simply adding whitespace doesn't have any impact on the data.
However, if you add white space between a double-quoted string, that will change the data.
Nested Data
Most JSON files contain lots of nested data. This is driven by the structure of the data.
For example, we could have an array of artists. The name is artists and the value is an array containing details of various artists. Each artist can have its own an array of albums, each with various name/value pairs.
So to expand on that example, here's an example with three artists.