# Read JSON file in CERN ROOT

**URL:** <https://root-forum.cern.ch/t/read-json-file-in-cern-root/50769>\
**Category:** ROOT\
**Tags:** tree, root\
**Created:** [July 12, 2022, 2:11pm UTC](https://root-forum.cern.ch/t/read-json-file-in-cern-root/50769 "2022-07-12T14:11:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ASHISH\_SEHRAWAT](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ashish_sehrawat/32/19883_2.png) [@ASHISH\_SEHRAWAT](https://root-forum.cern.ch/u/ASHISH_SEHRAWAT)\
**Post date:** [July 12, 2022, 2:11pm UTC](https://root-forum.cern.ch/t/read-json-file-in-cern-root/50769/1 "2022-07-12T14:11:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [July 12, 2022, 5:24pm UTC](https://root-forum.cern.ch/t/read-json-file-in-cern-root/50769/2 "2022-07-12T17:24:00Z")

</div>

Hi,

You can use [nlohmann/json.hpp](https://github.com/nlohmann/json#serialization--deserialization) parser which is also used in ROOT. Like:

```auto
#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

---

<div class="post-metadata">

**Author:** ![ASHISH\_SEHRAWAT](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ashish_sehrawat/32/19883_2.png) [@ASHISH\_SEHRAWAT](https://root-forum.cern.ch/u/ASHISH_SEHRAWAT)\
**Post date:** [July 12, 2022, 7:45pm UTC](https://root-forum.cern.ch/t/read-json-file-in-cern-root/50769/3 "2022-07-12T19:45:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [July 12, 2022, 8:36pm UTC](https://root-forum.cern.ch/t/read-json-file-in-cern-root/50769/4 "2022-07-12T20:36:29Z")

</div>

Hi Ashish,

```auto
    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++](https://github.com/nlohmann/json#stl-like-access). We are not developing this parser - but just using it.

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![ASHISH\_SEHRAWAT](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ashish_sehrawat/32/19883_2.png) [@ASHISH\_SEHRAWAT](https://root-forum.cern.ch/u/ASHISH_SEHRAWAT)\
**Post date:** [July 13, 2022, 3:03am UTC](https://root-forum.cern.ch/t/read-json-file-in-cern-root/50769/6 "2022-07-13T03:03:55Z")

</div>

Hi Sergey,

It worked. Thank you very much.

Regards,  
Ashish

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [July 27, 2022, 3:04am UTC](https://root-forum.cern.ch/t/read-json-file-in-cern-root/50769/7 "2022-07-27T03:04:54Z")

</div>

Closed on July 27, 2022, 3:04am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
