What is JSON?

JSON is short for "JavaScript Object Notation"; originally meant for web applications, it has become the go-to data format for many environments and platforms. Its structuring makes for small, text-based, outputs that are easily parsable programmatically while still being human legible.

JSON is NOT a functional data notation. JSON data does not have a means to specify directives(commands) a program must do when attempting to understand the JSON data; the JSON standard strictly prohibits such as a means of ensuring security.

Whitespace

Previously it was mentioned that JSON is human readable yet most web API's return it as a jumbled mess. This is because JSON interpreters are required to ignore whitespace so it can be removed without costing data integrity; this is what those APIs do with the intent of reducing network traffic.

There are online tools that will re-add whitespace to make the output human readable.

Primitives

JSON has four primitive data types to represent simple values. These data types do not have the complexity or structuring to contain multiple values the way Container Types do.

Null

- Denoted by the keyword null

Null is used when there is absolutely no value to reference. That is, the value isn't empty text or 0 or false; there is absolutely nothing to reference.

Boolean

- Denoted by the keywords true or false

Boolean is used when a value is meant to only have two states. Its used to say yes, true, on, no, false, or off

Number

- Denoted by a typical numerical sequence

Number is used when a value should be interpreted as a number. Positive numbers, negative numbers and decimals are all Number data types.

String

- Denoted by text wrapped in double quotes ("")

String is used when a value, regardless of its contents should be interpreted as text.

Due to strings being able to contain any text, including double quotes, there are special character sequences that have alternative meanings within the context of a string:

  • \" - Represents a double quote
  • \/ - Represents a forward slash
  • \\ - Represents a back slash
  • \f - Represents a form-feed character
  • \b - Represents a back-space character
  • \r - Represents a carriage return character
  • \n - Represents a line-feed character
  • \t - Represents a tab character
  • \uNNNN - Represents a Unicode character where NNNN is the hexadecimal code point.

Containers

Containers, as their name implies, are used to contain multiple values and can consist of both primitive and container datatypes.

Array

- Denoted by a comma separated list of values wrapped in brackets ([])

Arrays are ordered lists of values where each value is automatically given an index for referencing. Indexes start at zero and increase by one for each value; the first value uses an index of 0, the second an index of 1, and so on for each value in the array.

Object

- Donated by a comma separated list of key-value pairs wrapped in braces ({})

Objects are unordered lists where each value contained within is referenced by a given key. All keys are represented as a string and are separated from their value via the use of a colon (:). Keys are case sensitive so attempting to reference "KEY" will not point to the data contained under "key".

Accessing

Accessing specific values within JSON data is fairly straight forward once the various data types are understood.
In the following examples green colored lines are comments, and represents referencing an item of a container.

Primitives

If the accessed JSON member's value is a primitive the value is simply returned by the interpreter.

Containers

If the JSON data to access is a container a key or index must be specified.

Parse the input json; notice that its an array
jsondata = parse_json([
    "item0",
    "item1"
])

Retrieves the 1st item(at index 0) of the parsed array and outputs the text: item0
result = jsondata → 0
output(result)

Retrieves the 2nd item(at index 1) of the parsed array and outputs the text: item1
result = jsondata → 1
output(result)

This would result in an error because there isn't an item at index 2 since indexes start at 0
result = jsondata → 2
Parse the input json; notice that its an object
jsondata = parse_json({
    "key0": "value0",
    "key1": "value1"
})

Retrieves the value at key0 and outputs the text: value0
result = jsondata → key0
output(result)

Retrieves the value at key1 and outputs the text: value1
result = jsondata → key1
output(result)

This would result in an error because the key doesn't exist
result = jsondata → key2

This would result in an error because keys are case sensitive
result = jsondata → KEY0

Nested Data

If the JSON data has nested containers, each index/key must be specified to get to the data you wish to access.

input = {
    "key1": "value",
    "key2": [
        "index0",
        "index1"
    ],
    "key3": {
        "key3.1": "value2"
    },
    "key4": {
        "key4.1": [
            "value3"
        ]
    }
}

jsondata = parse_json(input)

Results in 'myvar' being set to the text: value
myvar = jsondata → key1

Looks up 'key2' in the json data then within key2's value the item at index-0 is looked up resulting in 'myvar' being set to the text: index0
myvar = jsondata → key2 → 0

Same as above but the item's value at index-1 is returned resulting in 'myvar' being set to the text: index1
myvar = jsondata → key2 → 1

Looks up 'key3' in the json data then within key3's value looks up 'key3.1' resulting in 'myvar' being set to the text: value2
myvar = jsondata → key3 → key3.1

Looks up 'key4' in the json data then within key4's value looks up 'key4.1' then within key4.1's value, looks up the item at index-0 resulting in 'myvar' being set to the text: value3
myvar = jsondata → key4 → key4.1 → 0
SReject © 2016-2017