Skip to main content

How to Put a JSON Object Inside Another JSON Object

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A value in a JSON object can be another object, so related details stay together: a book's author can be an object with a first and a last name. The inner object follows the same rules as the outer one, and every curly bracket that opens must close.

Syntax​

General form
{
"<name>": {
"<inner name>": <value>,
"<inner name>": <value>
}
}
  • "<name>": { … }: a pair whose value is a whole object, in its own curly brackets.
  • "<inner name>": <value>: the inner object's pairs, written like any object's.
  • The brackets: each { needs its own }. The inner object closes before the outer one.

The JSON standard, RFC 8259, allows it because an object is a value: A JSON value MUST be an object, array, number, or string, or one of the following three literal names: false null true, and an object's pairs hold values.

The Example​

A file called book.json describes the novel Emma: its title, and its author as an object with first and last. 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 Objects​

Create a file called book.json with this text:

book.json, a JSON object with title Emma and author, whose value is an inner object with first Jane and last Austen

book.json
{
"title": "Emma",
"author": {
"first": "Jane",
"last": "Austen"
}
}

The outer object has two pairs, title and author. The comma after "Emma" separates them. Inside author, the comma after "Jane" separates the inner pairs.

Step 2: Check It with jq​

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

Terminal
jq . book.json

jq's manual explains the .: the . program's main use is to validate and pretty-print the inputs. The file was valid, so jq printed both objects back, the inner one indented under author:

Output
{
"title": "Emma",
"author": {
"first": "Jane",
"last": "Austen"
}
}

Common Mistakes​

I left out one of the two closing brackets at the end of a copy of book.json and ran jq . on it. jq 1.8.2 said:

Output
jq: parse error: Unfinished JSON term at EOF at line 7, column 0

EOF is the end of the file: jq reached it while an object was still open.

I tried the other two mistakes the same way. These are jq's messages for all three:

MistakeWhat jq saidDo this instead
One } too fewUnfinished JSON term at EOF at line 7, column 0Close the inner object, then the outer one
A comma after the inner object's last pairExpected another key-value pair at line 6, column 3No comma before a closing bracket
The inner object without its nameObjects must consist of key:value pairs at line 6, column 1Write "author": { … }

Troubleshooting​

SymptomLikely causeFix
Unfinished JSON term at EOFA closing bracket is missingCount the { and }: they must match
Expected separator between valuesA missing comma between the outer pairsAdd the comma before the next name
The inner pairs print at the outer levelThey were written outside the inner bracketsMove them inside { }

FAQ​

How deep can objects go?​

The standard sets no limit on nesting, but it lets software set one: An implementation may set limits on the maximum depth of nesting. A few levels are easy to read.

Can the inner object be empty?​

Yes. "author": {} is an object with no pairs, which is still valid.

Can an object hold an array too?​

Yes. An array is a value like any other: jq . accepted "tags": ["classic", "novel"] as a pair in the same kind of object.

Conclusion​

To put a JSON object inside another, use it as a value: "author": { "first": "Jane", "last": "Austen" }. The inner pairs follow the same rules, and each { needs its }. jq . book.json printed the nested object back, and a missing bracket gave Unfinished JSON term at EOF.

Additional Resources​