14.2.2 Inspecting JSON Syntax
Review JSON objects, arrays, and value types used in REST API payloads.
- Objects – collections of name/value pairs, enclosed in curly braces
{}, and - Arrays – ordered lists of values, enclosed in square brackets
[].
{ 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
}[ 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.
[
{ "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.
Parent topic: Reviewing REST API Basics