Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing elements from nlohmann json

Tags:

c++

json

My JSON file resembles this

{
  "active" : false,
  "list1" : ["A", "B", "C"],
  "objList" : [
    {
     "key1" : "value1",
     "key2" : [ 0, 1 ]
    }
   ]
}

Using nlohmann json now, I've managed to store it and when I do a dump jsonRootNode.dump(), the contents are represented properly.

However I can't find a way to access the contents.

I've tried jsonRootNode["active"], jsonRootNode.get() and using the json::iterator but still can't figure out how to retrieve my contents.

I'm trying to retrieve "active", the array from "list1" and object array from "objList"

like image 345
Jeremy Kuah Avatar asked Jun 29 '16 12:06

Jeremy Kuah


3 Answers

The following link explains the ways to access elements in the JSON. In case the link goes out of scope here is the code

#include <json.hpp>

 using namespace nlohmann;

 int main()
 {
     // create JSON object
     json object =
     {
         {"the good", "il buono"},
         {"the bad", "il cativo"},
         {"the ugly", "il brutto"}
     };

     // output element with key "the ugly"
     std::cout << object.at("the ugly") << '\n';

     // change element with key "the bad"
     object.at("the bad") = "il cattivo";

     // output changed array
     std::cout << object << '\n';

     // try to write at a nonexisting key
     try
     {
         object.at("the fast") = "il rapido";
     }
     catch (std::out_of_range& e)
     {
         std::cout << "out of range: " << e.what() << '\n';
     }
 }
like image 101
Jeremy Kuah Avatar answered Nov 10 '22 12:11

Jeremy Kuah


In case anybody else is still looking for the answer.. You can simply access the contents using the same method as for writing to an nlohmann::json object. For example to get values from json in the question:

{
  "active" : false,
  "list1" : ["A", "B", "C"],
  "objList" : [
    {
      "key1" : "value1",
      "key2" : [ 0, 1 ]
    }
  ]
}

just do:

nlohmann::json jsonData = nlohmann::json::parse(your_json);
std::cout << jsonData["active"] << std::endl;    // returns boolean
std::cout << jsonData["list1"] << std::endl;     // returns array

If the "objList" was just an object, you can retrieve its values just by:

std::cout << jsonData["objList"]["key1"] << std::endl;    // returns string
std::cout << jsonData["objList"]["key2"] << std::endl;    // returns array

But since "objList" is a list of key/value pairs, to access its values use:

for(auto &array : jsonData["objList"]) {
    std::cout << array["key1"] << std::endl;    // returns string
    std::cout << array["key2"] << std::endl;    // returns array
}

The loop runs only once considering "objList" is array of size 1.

Hope it helps someone

like image 33
B8ightY Avatar answered Nov 10 '22 13:11

B8ightY


I really like to use this in C++:

for (auto& el : object["list1"].items())
{
  std::cout << el.value() << '\n';
}

It will loop over the the array.

like image 33
Melroy van den Berg Avatar answered Nov 10 '22 13:11

Melroy van den Berg