You can access elements in several different ways. This link covers all access methods supported by nlohmann::json. Below, I’ll explain in short using the same example you have provided:
// Source - https://stackoverflow.com/q/38099308
// Posted by Jeremy Kuah
// Retrieved 2025-11-30, License - CC BY-SA 3.0
{
"active" : false,
"list1" : ["A", "B", "C"],
"objList" : [
{
"key1" : "value1",
"key2" : [ 0, 1 ]
}
]
}
Using operator[]: This works similar to accessing elements in a std::map. For example, to read the value of "active", you can just write json["active"] (assuming your JSON object is named json). To access the first element of "list1", you can index into it like this: json["list1"][0].
using at(): Similar to above, use .at() instead of []. For eg, json.at("active") or something like json.at("list1").at(0).
using value(): This method is useful because it first checks whether the key exists. If the key is missing, you can provide a default value that will be returned instead. For example, json.value("active", true) will return the actual value of "active", while json.value("notSoActive", true) will return the default value true since the key doesn’t exist. Bonus tip: If I have to do the same thing using the above [] operator, I can do it this way:
try
{
auto& returnValue = json["notSoActive"];
}
catch (const json::out_of_range& e)
{
std::cout << "message: " << e.what() << '\n'
<< "exception id: " << e.id << std::endl; // return the default value
}