JSON Input outout using ROOT trees

How do you create a dynamic structure ROOT tree using data read from a JSON file with multiple data layers (arrays and objects within objects etc.) ?

I am currently in progress of making a tree and filing it with data parsed from a JSON file using the JSONcpp libraries in C++., but am having issues in automatically creating a class that replicates the structure of the JSON file in the first run through the file and then fills the tree in the second run through the data. At the moment, I have managed to achieve a hard coded versioon of this where the root values (head nodes) have to called by the program and the sub entities have to be defined. Its works well for a specific JSON files only but I want it to be able to extract and fill data into a tree from any JSON file.

Any ides on how to go about doing this please? any help would be really appreciated.



Assuming you have some code that can take the JSON file and generate the class definition, then at run-time, simply write the class definition into a file (json_file_struct.C) and load this file via ACLiC (gROOT->ProcessLine(".L json_file_struct.C+)).


I am not able to generate the structure class definition. I made an event class that defines the data structure manually. One of the issues is trying to generate the class with the structure automatically.


Hi Prash,

The structure of a JSON file can be broken down to a few elements. At the bottom are classes that would contains just 'long long, unsigned long long, bool, double and strings). Classes can also contain collection of other classes (you could settle on just std::vector there of).

Using the interface described at jsoncpp.sourceforge.net/class_js … value.html
you should be able traverse the JSON document down to the classes containing only simple type, create those and record their name. Take one step back up and use those classes in the containing class either directory or as std::vector of, in the containing classes. Continuing back up the chain until you reach the top level. And voila you have the complete set of classes :slight_smile:.