14.2.2 Inspecting JSON Syntax

Review JSON objects, arrays, and value types used in REST API payloads.

JavaScript Object Notation (JSON) is a lightweight, text-based format for representing structured data. It's easy for humans to read and write, and simple for programs to parse and generate. JSON describes data using two main structures:
  • Objects – collections of name/value pairs, enclosed in curly braces {}, and
  • Arrays – ordered lists of values, enclosed in square brackets [].
A JSON object begins with { and ends with }. Inside, one or more name/value pairs appear, separated by commas:
  • You always write names – also called properties or keys – in double quotes "…".
  • A value follows each name, separated by a colon.

For example, the object below represents a team member named Georgia. The keys are name, role, user_id, and active. The values "Georgia" and "LEAD" are strings, while 5 is a number and true is a boolean (true or false) value.

{
  "name": "Georgia",
  "role": "LEAD",
  "user_id": 5,
  "active": true
}

The order of the properties in a JSON object is not significant, so the following object is the same logical object as above, even though their two text representations are not exactly equal.

{
  "user_id": 5,
  "role": "LEAD",
  "name": "Georgia",
  "active": true
}
A JSON array starts with [ and ends with ]. Its elements can be strings, numbers, booleans, null, objects, or other arrays. Commas separate multiple elements. For example, an array of strings looks like:
["apple","banana","cherry"]

Tip:

In contrast to the ordering of object properties, array element order is significant. However, some apps may ignore that ordering.

Arrays often hold objects. For example, this is an array of two team members:
[
  { "name": "Georgia", "role": "LEAD",   "user_id": 5, "active": true},
  { "name": "Karl",    "role": "MEMBER", "user_id": 8, "active": true}
]

In JSON, a value can be one of the types below.

Table 14-2 JSON Types and Their Representations

Type Textual Representation
String "some text"
Number 42 or 3.14 (no quotes)
Boolean true or false
Null null
Object {…}
Array […]

For better readability, use any spaces, tabs, or line breaks you need. Parsers ignore this whitespace.