How to use JSON

JSON stands for JavaScript Object Notation

JSON is a text format for storing and transporting data

JSON is "self-describing" and easy to understand

Syntax

This is an example of an object having 3 attributes defined:

  • name - String (Text)

  • age - Number

  • isAdult - Boolean (True/False)

  • chores - List<String> (A list of String)

Every object is defined by {} and will contains every attribute in the following format:

{
  "attributeName": value,
  "attributeName": value,
  "attributeName": value
}

A String will be quoted with " on each side.

A Number can just be written directly.

A Boolean must be either true or false (in lower case).

A List must start and end with [ and ] respectively, with elements spaced with a ,.

{
    "name": "John",
    "age": 30,
    "isAdult": true,
    "chores": ["cleaning", "trash"]
}

JSON don't require any spacing format, which means you can put spaces or tabs everywhere, it will still works:

It's what we mostly use as it's the most human readable.

{
    "name": "John",
    "age": 30,
    "isAdult": true,
    "chores": [
        "cleaning", 
        "trash"
    ]
}

Also, an object can contain another one:

{
    "name": "John",
    "age": 30,
    "isAdult": true,
    "wear": {
        "name": "pants",
        "color": "red",
        "shorts": false
    }
}

Editor

You can edit JSON files with any basic text editor, but it's going to be far easier to avoid mistakes when using an editor. There's an example of one online: https://jsoneditoronline.org/

Last updated