Working with JSON in C++ — Objects, Arrays, Nested Data, and Recursive Traversal
JSON is a tree of Objects, Arrays, and Values. Once you understand how to navigate that tree recursively, you can read/write almost any…
Working with JSON in C++ — Objects, Arrays, Nested Data, and Recursive Traversal
JSON is a tree of Objects, Arrays, and Values. Once you understand how to navigate that tree recursively, you can read/write almost any JSON structure.
1. JSON Has Only a Few Building Blocks
When working with a JSON library, don’t think of JSON as a complicated format.
Think of it as a combination of:
Object
Array
String
Number
Boolean
Null
For example:
{
"name": "Anuj",
"age": 30,
"active": true
}
This is an Object.
An object contains:
key → value
So conceptually:
Object
|
+-- "name" → "Anuj"
+-- "age" → 30
+-- "active" → true
2. JSON Object
A JSON object is represented using {}.
{
"name": "Anuj",
"age": 30
}
You access values using the key:
json["name"];
json["age"];
Conceptually:
json
|
+-- name → "Anuj"
|
+-- age → 30
So:
string name = json["name"];
int age = json["age"];
The important idea is:
Object → access using a key.
3. JSON Array
A JSON array is represented using [].
[
"C++",
"Qt",
"Python"
]
You access an array using an index:
json[0];
json[1];
json[2];
Conceptually:
Array
|
+-- [0] → "C++"
+-- [1] → "Qt"
+-- [2] → "Python"
The important idea is:
Array → access using an index.
4. Object Containing an Array
This is where JSON becomes interesting.
{
"name": "Anuj",
"skills": [
"C++",
"Qt",
"CMake"
]
}
The root is an object.
Inside that object:
"name" → string
"skills" → array
So:
json["name"];
json["skills"];
And because skills is an array:
json["skills"][0];
json["skills"][1];
For example:
string skill = json["skills"][0];
5. Array of Objects
This is one of the most common JSON structures.
{
"employees": [
{
"name": "Anuj",
"age": 30
},
{
"name": "Rahul",
"age": 28
}
]
}
The structure is:
Object
|
+-- "employees"
|
v
Array
|
+-- Object
| |
| +-- name → Anuj
| +-- age → 30
|
+-- Object
|
+-- name → Rahul
+-- age → 28
Now:
json["employees"]
returns an array.
Therefore:
json["employees"][0]
returns the first object.
Then:
json["employees"][0]["name"]
returns:
"Anuj"
And:
json["employees"][1]["age"]
returns:
28
This gives us a very useful rule:
Object → key
Array → index
6. The Most Important Pattern
When navigating JSON, keep asking:
What type is the current JSON value?
For example:
json["employees"][0]["name"]
Walk through it:
json
↓
Object
↓
["employees"]
↓
Array
↓
[0]
↓
Object
↓
["name"]
↓
String
So the navigation is:
Object → Array → Object → String
This mental model is extremely useful.
7. Nested Objects
Consider:
{
"user": {
"name": "Anuj",
"address": {
"city": "Noida",
"country": "India"
}
}
}
You can navigate:
json["user"]["name"];
or:
json["user"]["address"]["city"];
The tree looks like:
Object
|
+-- user
|
+-- name → Anuj
|
+-- address
|
+-- city → Noida
+-- country → India
Again:
Object → key → Object → key → value
8. Object → Array → Object → Array
Real-world JSON can become deeply nested.
For example:
{
"companies": [
{
"name": "CompanyA",
"employees": [
{
"name": "Anuj",
"skills": ["C++", "Qt"]
},
{
"name": "Rahul",
"skills": ["Java", "Spring"]
}
]
}
]
}
You could access:
json["companies"][0]["employees"][0]["skills"][0];
Result:
C++
Trace it:
Object
↓
companies
↓
Array
↓
[0]
↓
Object
↓
employees
↓
Array
↓
[0]
↓
Object
↓
skills
↓
Array
↓
[0]
↓
"C++"
Once you understand this, even complicated JSON becomes manageable.
9. Saving JSON
JSON libraries generally allow you to construct JSON programmatically.
Conceptually:
json person;
person["name"] = "Anuj";
person["age"] = 30;
person["active"] = true;
This produces:
{
"name": "Anuj",
"age": 30,
"active": true
}
You can also create arrays.
json skills;
skills.push_back("C++");
skills.push_back("Qt");
skills.push_back("CMake");
Result:
[
"C++",
"Qt",
"CMake"
]
Then put that array inside an object:
person["skills"] = skills;
Result:
{
"name": "Anuj",
"age": 30,
"active": true,
"skills": [
"C++",
"Qt",
"CMake"
]
}
10. Creating an Array of Objects
Suppose we want:
{
"employees": [
{
"name": "Anuj",
"age": 30
},
{
"name": "Rahul",
"age": 28
}
]
}
Conceptually:
json employees;
json employee1;
employee1["name"] = "Anuj";
employee1["age"] = 30;
json employee2;
employee2["name"] = "Rahul";
employee2["age"] = 28;
employees.push_back(employee1);
employees.push_back(employee2);
json company;
company["employees"] = employees;
The important concept is:
employees
↓
array
↓
object
↓
key/value
11. The Cleaner Way
Most JSON libraries allow you to construct this more naturally:
json company = {
{"employees", {
{
{"name", "Anuj"},
{"age", 30}
},
{
{"name", "Rahul"},
{"age", 28}
}
}}
};
The exact syntax depends on the library, but the underlying concept remains:
Object
|
+-- employees
|
+-- Array
|
+-- Object
|
+-- Object
12. Reading an Array of Objects
Suppose we have:
{
"employees": [
{
"name": "Anuj",
"age": 30
},
{
"name": "Rahul",
"age": 28
}
]
}
We can iterate:
for (auto& employee : json["employees"])
{
string name = employee["name"];
int age = employee["age"];
cout << name << " " << age;
}
Conceptually:
json["employees"]
|
v
Array
|
+---- employee 0
|
+---- employee 1
For every element:
employee
↓
Object
↓
employee["name"]
employee["age"]
13. Why Recursion Is Useful
Now imagine we don’t know the structure beforehand.
For example, we receive:
{
"user": {
"name": "Anuj",
"skills": [
"C++",
"Qt"
],
"address": {
"city": "Noida"
}
}
}
We want to print every value, regardless of how deeply nested it is.
Hardcoding:
json["user"]["skills"][0]
isn’t useful.
We need a generic solution.
This is where recursion becomes powerful.
14. Recursive JSON Traversal
The basic idea is:
If current value is an Object:
visit every key/value pair
If current value is an Array:
visit every element
Otherwise:
process the value
Pseudo-code:
visit(value):
if value is Object:
for each (key, child) in value:
print key
visit(child)
else if value is Array:
for each child in value:
visit(child)
else:
print value
That’s the entire recursive idea.
15. Example of Recursive Traversal
Given:
{
"name": "Anuj",
"skills": [
"C++",
"Qt"
],
"address": {
"city": "Noida"
}
}
The recursion behaves like:
visit(root)
|
+-- Object
|
+-- name
| |
| +-- "Anuj"
|
+-- skills
| |
| +-- Array
| |
| +-- "C++"
| +-- "Qt"
|
+-- address
|
+-- Object
|
+-- city
|
+-- "Noida"
The same function keeps calling itself.
16. Recursive Pseudo-Code With Depth
We can make the output easier to understand:
visit(value, depth):
if value is Object:
for each key/value:
print indent(depth) + key
visit(value, depth + 1)
else if value is Array:
for each element:
visit(element, depth + 1)
else:
print indent(depth) + value
For the previous JSON:
name
Anuj
skills
C++
Qt
address
city
Noida
17. Recursive Search
Recursion becomes even more useful when you want to find something without knowing where it is.
Suppose:
{
"company": {
"departments": [
{
"name": "Engineering",
"manager": {
"name": "Anuj"
}
}
]
}
}
We want to find every "name".
We can write:
find(value, targetKey):
if value is Object:
for each (key, child):
if key == targetKey:
process(child)
find(child, targetKey)
else if value is Array:
for each child:
find(child, targetKey)
Now it doesn’t matter whether "name" is:
root["name"]
or:
root["company"]["departments"][0]["manager"]["name"]
The recursive algorithm can find it.
18. Recursive Conversion to C++ Objects
Another very useful real-world pattern is converting JSON into C++ objects.
Suppose:
{
"name": "Anuj",
"age": 30
}
and:
struct Person
{
string name;
int age;
};
Then conceptually:
JSON Object
|
+-- name → C++ string
|
+-- age → C++ int
Pseudo-code:
Person fromJson(json):
Person p
p.name = json["name"]
p.age = json["age"]
return p
For an array:
JSON Array
|
+-- Object → Person
+-- Object → Person
+-- Object → Person
Pseudo-code:
vector<Person> fromJsonArray(array):
result = empty vector
for each element in array:
result.push_back(
fromJson(element)
)
return result
This pattern is extremely common in real applications.
19. JSON → C++ → JSON
A typical application has this flow:
JSON
|
v
Deserialize
|
v
C++ Objects
|
application logic
|
v
C++ Objects
|
v
Serialize
|
v
JSON
For example:
API Response
|
v
JSON
|
v
Employee objects
|
v
Application
|
v
Employee objects
|
v
JSON
|
v
API Request
This is one of the most important practical uses of JSON libraries.
20. A Simple Mental Model
Whenever you see JSON, ask only two questions:
Question 1
Is this an Object or an Array?
If Object:
use key
If Array:
use index / iteration
Question 2
What is inside it?
For example:
Object
|
+-- key → Object
means:
json["key"]["anotherKey"]
While:
Object
|
+-- key → Array
means:
json["key"][0]
And:
Object
|
+-- key → Array
|
+-- Object
means:
json["key"][0]["anotherKey"]
21. The Universal JSON Navigation Pattern
You can think of JSON navigation as alternating between two operations:
OBJECT → key
ARRAY → index
For example:
Object
↓ key
Array
↓ index
Object
↓ key
Array
↓ index
Value
becomes:
json["users"][0]["skills"][1]
This simple idea handles a surprisingly large amount of real-world JSON.
22. And When the Structure Is Unknown?
Use recursion.
visit(JSON value)
Object?
↓
visit every child
Array?
↓
visit every element
Primitive?
↓
process value
That gives us a universal JSON traversal algorithm:
JSON
|
+-------+-------+
| |
Object Array
| |
iterate keys iterate elements
| |
+-------+-------+
|
recurse
|
Primitive value
Final Mental Model
Don’t think:
“JSON is complicated.”
Think:
JSON
|
+-- Object
| |
| +-- key → value
|
+-- Array
| |
| +-- index → value
|
+-- Value
|
+-- String
+-- Number
+-- Boolean
+-- Null
+-- Object
+-- Array
And remember the two fundamental operations:
Object → ["key"]
Array → [index]
And once the depth or structure becomes unknown, recursion naturally solves the traversal problem.
메타데이터
- post_id
- b1d8dfd5f5cc
- slug
- working-with-json-in-c-objects-arrays-nested-data-and-recursive-traversal-b1d8dfd5f5cc
- url
- https://medium.com/@anujgupta394/working-with-json-in-c-objects-arrays-nested-data-and-recursive-traversal-b1d8dfd5f5cc
- canonical_url
- https://medium.com/@anujgupta394/working-with-json-in-c-objects-arrays-nested-data-and-recursive-traversal-b1d8dfd5f5cc
- author_url
- https://medium.com/@anujgupta394
- status
- ok
- fetched_at
- 2026-08-24 07:32:38