Skip to main content

How to Write an Array of Objects in JSON

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A list of records, such as the people in a team, is written in JSON as an array of objects: square brackets around one object per record. Each object uses the same names, and a comma separates one object from the next, just as it separates any two values in an array.

Syntax​

General form
[
{ "<name>": <value>, "<name>": <value> },
{ "<name>": <value>, "<name>": <value> }
]
  • [ and ]: the list, an array.
  • { … }: one record, an object with its own name/value pairs.
  • , between objects: the same comma that separates any two values in an array, and none after the last object.

Nothing new is needed for this: the JSON standard, RFC 8259, says An array structure is represented as square brackets surrounding zero or more values, and an object is one of the values a JSON value can be. Giving each object the same names is a convention that makes the list easy to use, not a rule of the format.

The Example​

A file called team.json lists a team of two: Ada, the lead, and Ben, the tester. Each record has a name and a role. jq, a command-line JSON tool, checks that it's valid.

Prerequisites​

  • jq, to check the file. Run jq --version to see which one you have; this guide uses jq 1.8.2 (Download jq).

Step 1: Write the List​

Create a file called team.json with this text:

team.json, a JSON array in square brackets holding two objects: name Ada with role Lead, and name Ben with role Tester, separated by a comma

team.json
[
{ "name": "Ada", "role": "Lead" },
{ "name": "Ben", "role": "Tester" }
]

Each record sits on its own line, which keeps a short list readable. The comma at the end of Ada's line separates the two objects.

Step 2: Check It with jq​

In a terminal, in the folder with the file, run:

Terminal
jq . team.json

jq's manual explains the .: the . program's main use is to validate and pretty-print the inputs. The list was valid, so jq printed it back, with each object's pairs on separate lines:

Output
[
{
"name": "Ada",
"role": "Lead"
},
{
"name": "Ben",
"role": "Tester"
}
]

Common Mistakes​

I removed the comma between the two objects in a copy of team.json and ran jq . on it. jq 1.8.2 said:

Output
jq: parse error: Expected separator between values at line 3, column 3

Line 3 is where Ben's object starts: jq expected a comma before it.

MistakeWhat happensDo this instead
No comma between objectsExpected separator between valuesAdd a comma after each object but the last
A comma after the last objectExpected another array element at line 4, column 1Remove it
Different names in each record, such as role and RoleThe list is valid, but software reading it finds the value under only one of the namesUse exactly the same names in every object

Troubleshooting​

SymptomLikely causeFix
Expected separator between values at the start of an objectThe comma before that object is missingAdd it
Expected another array elementA comma after the last objectRemove it
Unfinished JSON term at EOFThe closing ] or a } is missingClose every bracket you open

FAQ​

Can the objects have different pairs?​

Yes. The format doesn't compare objects in an array. Keeping the same names in every record is what makes a list easy to work with.

How do I write a list with no records?​

Use an empty array, [].

Where does this shape appear?​

Wherever a service returns several records. SharePoint's REST API, for example, returns a list's items as an array of objects called value, one object per item.

Conclusion​

An array of objects is [ { … }, { … } ]: one object per record, a comma between objects, and none after the last. jq . team.json printed both records back, and a missing comma between objects gave Expected separator between values at the line where the next object starts.

Additional Resources​