JSON Format
Last Updated: [last-modified] (UTC)
JSON, or JavaScript Object Notation, is a way of representing data. While its syntax comes from JavaScript, it is still language independent. In this article, we’re considering JSON from the perspective of REST.
JSON is typically used in two cases. One is to POST data to a web server. The other is a web server’s response to the client. Web servers may choose to use other formats, like XML, so don’t assume that every web server will use JSON.
There are two main advantages when using JSON with REST. For one, it’s very easy for a client to parse the file. Also, it is easily formatted in a way that’s easy for us humans to read.
JSON Structure
Below is a sample of some JSON data. This came from the currency conversion example in the REST article. If you are familiar with Python, you will notice that this looks like a dictionary.
{ "base": "GBP", "date": "2000-02-01", "rates": { "AUD": 2.5377, "HKD": 12.547, "USD": 1.613, "EUR": 1.6611 } }
The data consists of pairs of keys and values. There is a colon character separating each key and value. In the example, base is a key, and GBP is the corresponding value. A comma separates each pair.
Each value can be a different format, such as a string, an integer, and so on. see JSON Data Types for a full list and explanation.
Information within curly braces is an object. Some values may contain objects. In the example, the value for the rates key is another object, containing more pairs.
Here is another example, also taken from the REST article. For brevity, some of the data has been removed.
[ { "id": 26, "name": "Skull Candy", "abv": 3.5, }, { "id": 31, "name": "Nanny State", "abv": 0.5, }, { "id": 35, "name": "Berliner Weisse With Raspberries And Rhubarb - B-Sides", "abv": 3.6, }, ]
See how this starts and ends with square brackets? This indicates that the data is an array. Each object is a separate ‘record’ or element in the array.
Each object in the array is also separated by a comma.
References
Beginners Book – JSON Tutorial: Learn JSON in 10 Minutes
Tutorials Point – JSON Tutorial