Before we get to what it is, let's talk about why it exists:

Essence:

There are two main things we do with our personal computers and with the network of computers we call The Internet. We collect stuff (data) and we pass stuff around. Passing data around is one of the most common activities that happens on the Internet. So we need some way to format data that is common and understandable by many types of senders and receivers.

For example if you load your twitter profile, your browser sends a request and receives JSON in response. That JSON contains all of the stuff that gets printed on the webpage, in a format that the web page can read and understand. This data was stored on Twitter's servers in database tables, in rows and columns. But the browser needs a way to make sense of the data, along with the labels and what goes together. The essence of JSON data is key/value pairs. So with something like {"color": "green"}, color is the key and green is the value.

Definition of What The Thing Is:

Example JSON with Strings, Numbers, Arrays, and nested JSON as Values
Example JSON with Strings, Numbers, Arrays, and nested JSON as Values 

The acronym stands for JavaScript Object Notation. It's a human readable format for exchanging data. JSON is often used when data is sent from a server to a web page.

β€” JSON data is in key/value pairs. Data is separated by commas, curly braces {} hold objects, square brackets [] hold arrays.

β€” JSON values can be one of these types: string, number, boolean (true or false), array, another nested JSON object, or null.

πŸ₯‘ In the guacamole recipe JSON above, the key item has string values, the key quantity has number values, the key vegetarian has boolean value, the key ingredients has an array value, and the keys recipes and guacamole both have nested JSON values.

The JSON format looks the same as the code for creating objects in JavaScript. But JSON is not really tied to JavaScript. Reading and writing JSON can be done in any programming language.

Because of the similarity, a JavaScript program can easily convert JSON data into native JavaScript objects using JSON.parse(). If you receive data from a server, in JSON format, you can use it like any other JavaScript object.

Other tidbits / Connections:

β€” JSON is often used as the response format when data is requested from a service via an API call.

β€” YAML is very similar to JSON. It offers alternative syntax by replacing all JSON delimiters like {}, [], and " with indentation. YAML supports comments, while JSON does not.

β€” JSON and XML are similar in that they are both human readable, have nested values, and can be used by many programming languages. They are different in that JSON is shorter, quicker to read and write, and JSON can use arrays. A dedicated XML parser is needed to read XML. JSON can be parsed within a normal JavaScript function.

β€” JSON replaced XML in the acronym AJAX (Asynchronous JavaScript and XML) from the early 2000's.

That's all there is to it. Now you know the core concept, the essence of The Thing called JSON.


Additional Reference:

Command Line Formater:

jq is a tool for working with JSON in the command line. It adds line breaks and indentation. So that the whole JSON string is not a big jumble of characters across your terminal. The usage can look something like this:

curl 'https://api.example.com/resource' | jq '.'

Browser Extensions:

It is useful to be able to view JSON string with collapsible hierarchy within a browser. There are browser add-ons to be able to do this β€” Chrome JSON Formater and Firefox JSON View for example.

Code Examples

Most programming languages have a way to convert a String into JSON and to parse a JSON string into an object native to that programming language. Β Here are some examples of how to create and read JSON in various programming languages.

JavaScript:

const json = '{"result":true, "count":42}';
const obj = JSON.parse(json);

console.log(obj.count);
// expected output: 42

console.log(obj.result);
// expected output: true

Ruby:

require 'json'

# parse JSON
my_hash = JSON.parse('{"hello": "goodbye"}')
puts my_hash["hello"] => "goodbye"

# generate JSON
my_hash = {:hello => "goodbye"}
puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}"

Python:

import json

## parse JSON
json_string =  '{ "name":"John", "age":30, "city":"New York"}'

python_obj = json.loads(json_string)

# the result is a Python dictionary:
print(python_obj["age"])

## generate JSON
python_obj = {
  "name": "John",
  "age": 30,
  "city": "New York"
}

# convert into JSON:
json_string = json.dumps(python_obj)

# the result is a JSON string:
print(json_string)