JSON
Java Script Object Notation (JSON) is a data format that allows applications to communicate over networks in a simple syntax. JSON is used in Javascript (duh), Node.js, MongoDB (and other NoSQL databases).
Types of JSON documents
JSON documents can be the following two types:
Data Type | Surrounding Characters | Example |
---|---|---|
Array | [] square brackets |
[ "Hello", "JSON" ] |
Object | {} curly braces |
{"Hello":"JSON"} |
JSON Arrays
An array is simply a data structure with an ordering. All pairs except the last must be separated by commas
[
"zero",
"one",
"two"
]
Object
Objects are simply key value pairs, like the dictionary/map data structure. All keys must be unique, and all pairs except the last must be separated by commas.
{
"myString" : "value",
"nullValue": null,
"myBoolean": true,
"myNumber" : 0
}
JSON Schema
JSON Schema specifies the expected structure of JSON documents. This is used when a document must have a given structure, as in an API response.
Attached below is a fictitious programmer.json
schema, outlining the valid
format for a programmer
object submitted to this API.
{
"type": "object",
"properties": {
"firstName": { "type" : "string" },
"lastName": { "type" : "string"},
"age" : { "type" : "integer"},
"editor" : {
"name" : { "type" : "string" },
"isVim" : { "type" : "boolean" }
},
"projects" : { "type" : "array" }
},
"required": ["firstName", "lastName", "editor"]
}
A valid programmer, aaron.json
would be formatted as follows:
{
"firstName" : "Aaron",
"lastName" : "Cote",
"age" : null,
"editor" : {
"name" :"Eclipse",
"isVim" : false
},
"projects": []
}