# JSON to ROOT File

**URL:** <https://root-forum.cern.ch/t/json-to-root-file/50966>\
**Category:** Newbie\
**Created:** [July 26, 2022, 7:42am UTC](https://root-forum.cern.ch/t/json-to-root-file/50966 "2022-07-26T07:42:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [July 26, 2022, 7:42am UTC](https://root-forum.cern.ch/t/json-to-root-file/50966/1 "2022-07-26T07:42:30Z")

</div>

I have a python script (decoder) that gives out readable data in json format. I am trying to have that data in ROOT file format. I changed the filename.json to filename.root, but it did not work. Please help out on how to get the data in ROOT format. Thank you.

Since I already have a json file, if there is a way to convert that into ROOT format then that will work as well.

```auto
# top directory of the specific run Rawdata_XX_XX_2020. YOU NEED TO MODIFY IT ACCORDINGLY
runFolder = 'run02tri'
#dirToData = "/home/user1/WORK/pcb_readout/3view30degree/data/"+runFolder+"/"
dirToData = "/home/user/Desktop/CERN/Rawdata_05_11_2022/"+runFolder+"/"

# number of events to be analyzed from each bin file .Nowadays we save 100 event in each file. If this is not true, we may have a problem here
numberOfEventsInFile = 25

# bias voltage on the collection plane, in volts
collectionBias = 900

# cathode HV, in volts
cathodeHV = 27200 

# directory and name of the file that the info will be saved. saving in a json format for now.. I assume we have a folder with name "jsonData"
#dataOutputFile = '/home/user1/WORK/pcb_readout/3view30degree/jsonData/'+runFolder+'.json'
dataOutputFile = '/home/user/Desktop/CERN/data.root'

#### THIS FUNCTION DOES THE BINARY DATA TO JSON CONVERSION AND SAVING ####
saveBinToJSON(dirToData,numberOfEventsInFile,collectionBias,cathodeHV,dataOutputFile)

#### Then the analysis of the results. First load the output file
#f = open('/home/user1/WORK/pcb_readout/analysis/jsonData/single_data.json') 
f = open(dataOutputFile) 
data = json.load(f) 

f.close() 

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 26, 2022, 9:00am UTC](https://root-forum.cern.ch/t/json-to-root-file/50966/2 "2022-07-26T09:00:31Z")

</div>

Maybe @linev can help you?

---

<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 26, 2022, 9:20am UTC](https://root-forum.cern.ch/t/json-to-root-file/50966/3 "2022-07-26T09:20:03Z")

</div>

Hi,

ROOT natively supports only special JSON format for objects storage. See docu: [ROOT: TBufferJSON Class Reference](https://root.cern.ch/doc/master/classTBufferJSON.html)

But one always can read arbitrary JSON with any parser and store parsed data into ROOT file as tree or as any other objects. In the topic [Read JSON file in CERN ROOT](https://root-forum.cern.ch/t/read-json-file-in-cern-root/50769) shown example how nlohmann parser can be used with ROOT.

Regards,  
Sergy

---

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [July 26, 2022, 9:38am UTC](https://root-forum.cern.ch/t/json-to-root-file/50966/4 "2022-07-26T09:38:13Z")

</div>

Thank you for your reply. I read the link above but could not understand what they were talking about.

I have an idea on how to solve this problem. Please let me know if you think this is reasonable or not.

My json file is in this format.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/8/7/87c767b366761ac510a87a27aa92778e8e84de5e.png)  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/e/7/e747d254f674cd6b1163018a7ff30a48868b02aa.png)  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/9/a/9a222de423db9b3416ee9803c485e7289dd340e7.png)

Here the 0, 1, 2, … represents an individual event. Inside it are chn0, chn1, … which is the channel number taking the data at that time. Inside the channel are time ticks value and ADC.

I am thinking of opening the json file and iterating among these values and assigning tree, branches, leafs accordingly.

For instance, 0 (event) as a tree. Inside it are different channels (chn0, chn1, …) as a branch. Finally, save it as a root file. Is this a reasonable approach? Please let me know.

---

<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 26, 2022, 9:48am UTC](https://root-forum.cern.ch/t/json-to-root-file/50966/5 "2022-07-26T09:48:57Z")

</div>

In the mentioned topic is an example, how arbitrary JSON data can be parsed.  
But how such data can be converted into ROOT object - histogram, graph, collection, tree, collection - fully responsibility of the user.
