What is JSON?
JSON, or JavaScript Object Notation, is a minimal, readable format for structuring data. It is used primarily to transmit data between a server and web application, as an alternative to XML.
Example
Append ?format=json-pretty to the URL of any page on your Squarespace site and you'll see a deluge of JSON data. Here is a small sample of what that might look like:
{ "collection" : { "title" : "Blog", "description" : "This is a description of my blog.", "categories" : [ "Category-1", "Category-2" ] } }
Keys and Values
The two primary parts that make up JSON are keys and values. Together they make a key/value pair.
- Key: A key is always a string enclosed in quotation marks.
- Value: A value can be a string, number, boolean expression, array, or object.
- Key/Value Pair: A key value pair follows a specific syntax, with the key followed by a colon followed by the value. Key/value pairs are comma separated.
Let's take one line from the JSON sample above and identify each part of the code.
"title" : "Blog"
his example is a key/value pair. The key is "title" and the value is "Blog".
Types of Values
- Array: An associative array of values.
- Boolean: True or false.
- Number: An integer.
- Object: An associative array of key/value pairs.
- String: Several plain text characters which usually form a word.
Numbers, booleans and strings are self-evident, so we'll skip over those sections. Arrays and Objects are explained in more depth below.
Arrays
Almost every blog has categories and tags. In this example we've added a categories key, but the value might look unfamiliar. Since each post in a blog can have more than one category, an array of multiple strings is returned.
"collection" : { "title" : "Blog", "categories" : [ "Category-1", "Category-2" ] }
Objects
An object is indicated by curly brackets. Everything inside of the curly brackets is part of the object. We already learned a value can be an object. So that means "collection" and the corresponding object are a key/value pair.
"collection" : { "title" : "Blog" }
he key/value pair "title" : "Blog" is nested inside the key/value pair "collection" : { ... }. That's an example of a hierarchy in JSON data.