From WikiChip
JSON

JavaScript Object Notation (JSON) is a lightweight, human-readable, text-based, language and platform-independent data interchange format[1]. JSON uses a simple structure of objects consisting of key-value pairs. While its syntax is largely derived from JavaScript as the name implies, JSON is language and platform-independent. Due to its relatively simple syntax which makes it both fast and simple to parse, JSON became the data interchange format of choice among many popular programming languages.

The JSON format is formally described in RFC 4627 as well as in the Standard ECMA-404[2].

Data Types[edit]

JSON can represent the following types:

  • strings
  • numbers
  • booleans (true and false values)
  • null
  • objects
  • arrays

Overview[edit]

JSON's syntax is derived from the object literals of JavaScript[1]. A JSON object can be either an object or as an array.

Values[edit]

Values can be either a string, a number, true, false, an array, an object, or null.

Strings[edit]

Strings must be enclosed in a pair of double quotes character ("). Strings are a sequence of zero or more Unicode characters. For example: "abcd" or "". Literal quotation mark, reverse solidus, and the control characters (range U+0000-U+001F) must be properly escaped. Characters can be represented using their escape sequence represented as a six-character sequence a reverse solidus, followed by the lowercase letter u, followed by four hexadecimal digits that encode the character's code point. For example "\u005C".

Numbers[edit]

A number has an integer component which can be prefixed with a minus symbol. The integer part may be followed by an exponent or a fraction part. Octal or hex forms are not allowed for example. For example 123, 123.456, and 12.3e4 are valid.

Boolean[edit]

The boolean values true and false can be use.

Null[edit]

A null value can be represented using the null keyword.

Array[edit]

An array is simply an ordered sequence of zero or more values. Any value (including another array) is a valid element of an array. Arrays can be represented as square brackets ([ and ]) surrounding zero or more values. For example

[1, "2", 3.4, false, 5]

Likewise, arrays can contain objects and more arrays:

[[[[[3]]],[[[1]]]],3]

Arrays can be empty:

[]

Object[edit]

A JSON object is a very simple structure that is represented as a pair of curly brackets ({ and }) surrounding zero or more name/value pairs called members. The name part of the pair is represented as a string. A colon (:) is placed after each name, separating the name from its value. A comma is placed after a value if another name comes after it. For example, the following is a valid JSON object:

{
    "Name"   : "Bob",
    "Age"    : 54,
    "Score"  : 143.54,
    "Active" : true
}

The object above has 4 members: Name, Age, Score, and Active.

JSON Structure[edit]

A JSON file must be either an array or an object; it cannot be any other value such as a string. For example the following is a valid JSON:

[
    {
        "Name"   : "Bob",
        "Age"    : 54,
        "Score"  : 143.54,
        "Active" : true
    },
    {
        "Status": true,
        "Amount": 4e5
    }
]

Parsing JSON[edit]

Parsing a JSON formatted data can be done in many of the popular programming languages.

JavaScript[edit]

Parsing JSON in JavaScript can be done using JSON.parse() for example:

try {
    /* first example */
    JSON.parse('{"Name":"Bob","Age":54,"Score":143.54,"Active":true}');
    
    /* second example */
    JSON.parse('[{"Name":"Bob","Age":54,"Score":143.54,"Active":true},{"Status":true,"Amount":4e5}]');
} catch (e) {
    console.error("Parsing JSON failed:", e); 
}

Python[edit]

Parsing JSON in Python can be achieved using the json module:

import json
data = json.loads("""{"Name":"Bob","Age":54,"Score":143.54,"Active":true}""")
data2 = json.loads("""[{"Name":"Bob","Age":54,"Score":143.54,"Active":true},{"Status":true,"Amount":4e5}]""")

Java[edit]

Parsing JSON in Java can be done using the JsonReader class:

JsonReader jsonReader = Json.createReader(
  new StringReader(
    "[{\"Name\":\"Bob\",\"Age\":54,\"Score\":143.54,\"Active\":true},{\"Status\":true,\"Amount\":4e5}]"
  )
);
JsonArray array = jsonReader.readArray();
jsonReader.close();

References[edit]