17. Serialization
Programming Project 2021/22

17.3. JSON

The JavaScript Object Notation (JSON) is a lightweight text-based format for serializing and transmitting structured data over network connections.

It originates from the Javascript programming language, having been designed to be human-readable. You will see that JSON is easy to read and write.

Despite its Javascript roots, JSON can be easily used with many programming languages, including Java and Python.

JSON Example

{
   "book": [
      {
         "id":"01",
         "language": "Java",
         "edition": "third",
         "author": "Herbert Schildt"
      },
      {
         "id":"07",
         "language": "C++",
         "edition": "second",
         "author": "E.Balagurusamy"
      }
   ]
}

Usage

JSON is widely used in web APIs:

  • Spotify
  • Facebbok
  • Slack
  • Fitbit
  • ...

Here are some open examples we can easily inspect.

Syntax

JSON supports the following data structures.

  • Map
  • List

A map, usually called an object in JSON, is represented with curly braces ({}).

  • Each key/value pair in an object is represented by a quoted key, followed by a colon (:), and then a value.
  • Key/value pairs are separated by commas (,).
{
  "id": 1,
  "language": "Java",
  "edition": "third",
  "author": "Herbert Schildt",
}

A list, usually called an array in JSON, is represented with square brackets ([]).

  • Its elements are separated by commas (,).

We can create lists of strings,

[ "My", "name", "is", "JSON" ]

lists of numbers,

[ 1, 100, 1000 ]

lists of objects,

[
  {
    "id": 1,
    "language": "Java",
    "edition": "third",
    "author": "Herbert Schildt",
  },
  {
    "id": 7,
    "language": "C++",
    "edition": "second",
    "author": "E.Balagurusamy",
  }
]

and even a mix of datatypes.

[1, "John", true, [0, 1], { "id": 1 }, null ]

DataTypes

JSON supports the following data types:

Type Description
Number double- precision floating-point format in JavaScript
String double-quoted Unicode with backslash escaping
Boolean true or false
Array an ordered sequence of values
Value a string, a number, true or false, null
Object an unordered collection of key:value pairs
null empty

JSON versus XML

This JSON document...

{
  "book": [
    {
      "id": "01",
      "language": "Java",
      "edition": "third",
      "author": "Herbert Schildt"
    },
    {
      "id": "07",
      "language": "C++",
      "edition": "second",
      "author": "E.Balagurusamy"
    }
  ]
}

... is equivalent to this XML document:

<?xml version="1.0" encoding="UTF-8" ?>
<book>
  <id>01</id>
  <language>Java</language>
  <edition>third</edition>
  <author>Herbert Schildt</author>
</book>
<book>
  <id>07</id>
  <language>C++</language>
  <edition>second</edition>
  <author>E.Balagurusamy</author>
</book>