JavaScript Object Notation, or JSON, is a solution to the problem of how do we move data between systems, especially data between a server and a web client.
JSON is an open-standards, data interchange format that uses a serializable string of data to store information. It was initiated in the early 2000’s by Douglas Crockford, and became a standard in 2013. The creator’s wish was for a simple format that could be described on the back of a business card.
While JSON has JavaScript in its name, it is actually language independent and supported within a large number of languages including, but not limited to, C++, C#, Python, Java, R, and many more. Many languages have multiple libraries that can be used to access JSON data.
Like XML, JSON is designed to be self describing, but also lightweight, which makes it preferable to XML in situations where the data will have to be transmitted often, such as web services. The fact that it is stored in the same manner as JavaScript, means that it is easily read by web browsers which need to transmit data between a server and client, making it very efficient for both transmitting and processing.
JSON data is stored as a series of name / value pairs, and can include both objects and arrays for complex data types. The data is simply serialized so that can be transmitted. This also makes the JSON data human readable.
Since JSON uses the notation of JavaScript, and is a subset of the JavaScript, which means that it has the datatypes of JavaScript are usable in JSON.
number | A signed decimal number that may be a floating point and may use exponential E notation, but cannot include non-numbers such as NaN. The format makes no distinction between integer and floating-point. |
string | A sequence of zero or more Unicode characters. Strings are contained within double-quotation marks and support a backslash escaping syntax. |
boolean | true or false – no quotes are used to contain the value. |
array | An ordered list of zero or more values, each of which may be of any type. Arrays are contained within square brackets and use commas to separate the elements. |
object | An unordered collection of name–value pairs where the names (also called keys) are strings. Each key is unique within an object. Objects are contained within curly brackets and use commas to separate each pair, while within each pair the colon ‘:’ character separates the key or name from its value. |
null | Uses the keyword null to denote an empty, or missing, value. |
JSON is stored as an object, and thus uses a name value pair to store it’s values. The field name is right there, so it is self describing. Each item in the object is a separated by commas, and each name value pair is separated by a colon.
Since an object can be made up of any of the data types listed above, that means it can have arrays, or objects, which contain objects themselves. This allows for the data to get more and more complex with the data stored. This means storing and transmitting complex data types can be done with relative ease.
However, data within JSON cannot natively link to other data elements. For example, if you had a list of people, and you could list a spouse or child, you couldn’t link to another element in your JSON data, you’d have to provide either their full information twice, or another way of self referencing, such as an ID field.
Consider the following example of JSON data:
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [],
"spouse": null
}
Whitepace in JSON is ignored. However, it is common to indent fields with each level of indentation representing a level of depth in the data, to make it easier to read. It is also common to put each name-value pair on its own separate line for the same reason. However, it would still be legal to write the data as one lone line of text.
An Introduction to JSON was originally found on Access 2 Learn