Mikke.Learns

What is JSON?

January 27th, 2019

The way I understand it, JSON (JavaScript Object Notation) is a method for storing data and exchanging data from one platform to another by breaking the data down to text, using a specific format and syntax.

JSON is a way of structuring data, and so JSON is a data format. In order for the data to be usable, we have to make sure that the format and syntax are correct. Earlier (and still to some degree today) this was done using XML, but JSON has become the favored method because it’s much easier to read.

It grew out of a need for a real-time server-to-browser communication protocol without using plugins such as Flash or Java applets, the predominant methods used in the late 90s and the first half of the 2000s.

So JSON is used for asynchronous browser-server communication and consists of an ordered list of key/value pairs:

{ "key": value } { "firstName": "Thomas" }

  • It extended from JavaScript

  • The media type is application/json

  • The extension is .json

JSON is an acronym for JavaScript Object Notation. It’s not JavaScript, but it’s based on how JavaScript creates objects (a representation of things in the world). JSON is, as such, an object which can be used to describe something. It’s a text-based, language-independent data transfer format like CSV, XML or YAML.

JSON always starts and ends with brackets. The key/value pairs are separated with a colon (:). The key must always be in double quotes (“”), but the value can be written without, as long as it’s not a string. Objects are comma (,) separated, and arrays are enclosed in square brackets ([]):

"phoneNumbers:" [ { "type": "home", "number": "555-555-555" }, { "type": "office", "number": "555-444-555" } ] In the example below, JSON is used to store information about a person. We can have as many levels of lists as needed for the organization of the data. { "firstName": "Thomas", "lastName": "Anderson", "age": 31, "address": { "streetAddress": "Hackerstreet 3", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "phoneNumbers:" [ { "type": "home", "number": "555-555-555" }, { "type": "office", "number": "555-444-555" }, { "type": "mobile", "number": "555-333-555" } ], "children": [], "spouse": "Trinity", "theOne": true } Here is an XML for comparison: <address> <city>New York</city> <postalCode>10021-3100</postalCode> <state>NY</state> <streetAddress>Hackerstreet 3</streetAddress> </address> <age>31</age> <children /> <firstname>Thomas</firstname> <lastName>Anderson</lastName> <phoneNumbers> <element> <number>555-555-555</number> <type>home</type> </element> <element> <number>555-444-555</number> <type>office</type> </element> <element> <number>555-333-555</number> <type>mobile</type> </element> </phoneNumbers> <spouse>Trinity</spouse> <theOne>true</theOne>

JSON is mostly used in APIs since it’s so lightweight, and it works seamlessly between serverside languages and the front end. It’s also compatible with most programming languages. It works very well with AJAX and has become the preferred method for receiving data. JSON is easy to read for humans, loads fast and is easily scalable. It’s compatible with the following data types:

  • Numbers

  • Strings

  • Boolean

  • Arrays

  • Objects

  • Null

JSON, like JavaScript objects, transfers data in key/value pairs as described above, but unlike JavaScript, JSON’s object keys must be defined between double quotes (“”). And duplicate keys returns “undefined”.

So, to summarize, these are the pros of JSON:

  • It’s fast

  • It’s easy to read

  • Works with both server-side languages and on the front end.

  • Understood by most programming languages.

  • The preferred method for transferring data between platforms.

  • Text-based and language independent.

  • Works great with AJAX

  • Easily scalable

  • Compatible with common data types.