Read JSON file in CERN ROOT

Hi all,

Can somebody please tell me how to read a JSON file in c++ and store each entry (hfoc15PaperV0, 246…, [25, 25], etc) in separate c++ objects?

Format of the JSON file is

[“hfoc15PaperV0”, {“246906”: [[25, 25], [27, 29], [31, 31], [33, 33], [35, 35], [38, 38], [41, 41]]}],
[“hfoc15PaperV0”, {“246908”: [[2, 2], [5, 6], [8, 8], [12, 12], [18, 19], [21, 21], [30, 30], [32, 32], [35, 35], [38, 38], [42, 42], [44, 44], [47, 225]]}],
[“hfoc15PaperV0”, {“246912”: [[1, 25]]}],
[“hfoc15PaperV0”, {“246913”: [[1, 10], [15, 30], [49, 68]]}],
[“hfoc15PaperV0”, {“246919”: [[1, 58]]}],
[“hfoc15PaperV0”, {“246920”: [[1, 25]]}],
[“hfoc15PaperV0”, {“246923”: [[1, 39]]}],
[“hfoc15PaperV0”, {“246926”: [[1, 274]]}],
[“hfoc15PaperV0”, {“246930”: [[1, 30], [46, 115]]}],
[“hfoc15PaperV0”, {“246933”: [[1, 25]]}],
[“hfoc15PaperV0”, {“246934”: [[1, 10]]}],
[“hfoc15PaperV0”, {“246935”: [[1, 7]]}],
[“hfoc15PaperV0”, {“246936”: [[1, 178]]}],
[“hfoc15PaperV0”, {“246943”: [[131, 197]]}],
[“hfoc15PaperV0”, {“246951”: [[1, 159]]}],
[“hfoc15PaperV0”, {“246953”: [[1, 37]]}],
[“hfoc15PaperV0”, {“246957”: [[1, 11]]}],
[“hfoc15PaperV0”, {“246958”: [[1, 71]]}],
[“hfoc15PaperV0”, {“246959”: [[1, 98]]}],
[“hfoc15PaperV0”, {“246960”: [[1, 63]]}],
[“hfoc15PaperV0”, {“246961”: [[1, 13]]}],
[“hfoc15PaperV0”, {“246962”: [[1, 12]]}],
[“hfoc15PaperV0”, {“246963”: [[1, 431]]}],
[“hfoc15PaperV0”, {“247066”: [[39, 43], [49, 65]]}],
[“hfoc15PaperV0”, {“247068”: [[1, 154]]}],
[“hfoc15PaperV0”, {“247069”: [[1, 11]]}],
[“hfoc15PaperV0”, {“247070”: [[1, 124]]}],
[“hfoc15PaperV0”, {“247073”: [[1, 357]]}],
[“hfoc15PaperV0”, {“247076”: [[1, 12]]}],
[“hfoc15PaperV0”, {“247077”: [[1, 13]]}],
[“hfoc15PaperV0”, {“247078”: [[1, 231]]}],
[“hfoc15PaperV0”, {“247079”: [[1, 93]]}],
[“hfoc15PaperV0”, {“247081”: [[1, 198]]}],
[“hfoc15PaperV0”, {“247222”: [[76, 91], [98, 110]]}],
[“hfoc15PaperV0”, {“247224”: [[1, 13]]}],
[“hfoc15PaperV0”, {“247225”: [[1, 15]]}],
[“hfoc15PaperV0”, {“247226”: [[1, 21]]}],
[“hfoc15PaperV0”, {“247227”: [[1, 17], [19, 21], [27, 38]]}],

Many thanks

Hi,

You can use nlohmann/json.hpp parser which is also used in ROOT. Like:

#include "nlohmann/json.hpp"
#include <iostream>

void try_json()
{
   auto j = nlohmann::json::parse("{\"happy\": true, \"pi\": 3.141}");

   std::cout << "pi = " << j.at("pi").get<double>() << std::endl;
}

Details you will find on library homepage.

Regards,
Sergey

Hi,

Can you please tell me what should I do to print only “246906”, 246908”, etc… (all entries in the second column) from the JSON file?

Thanks & regards,
Ashish

Hi Ashish,

    auto arr = nlohmann::json::parse("[[\"hfoc15PaperV0\", {\"246958\": [[1, 71]]}],"
                                      "[\"hfoc16PaperV0\", {\"246959\": [[1, 98]]}]]");

   for (auto &elem : arr) {
      std::cout << "str = " << elem.at(0).get<std::string>() << std::endl;
      auto &subelem = elem.at(1);
      for (auto &subsub : subelem) {
        std::cout << subsub.key() << " : " << subsub.value() << std::endl;
      }
   }

But be careful - your json is not fully valid. You need to add outmost array brackets [] to put all your elements inside.

And all relevant information about nlohmann/json.hpp library you can find on: GitHub - nlohmann/json: JSON for Modern C++. We are not developing this parser - but just using it.

Regards,
Sergey

Hi Sergey,

It worked. Thank you very much.

Regards,
Ashish