# I/O operations on JSON file

**URL:** <https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198>\
**Category:** ROOT\
**Tags:** root\
**Created:** [September 12, 2017, 4:07pm UTC](https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198 "2017-09-12T16:07:47Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![hep\_physics](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/53a042/32.png) [@hep\_physics](https://root-forum.cern.ch/u/hep_physics)\
**Post date:** [September 12, 2017, 4:07pm UTC](https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198/1 "2017-09-12T16:07:47Z")

</div>

Does root have the provision to do I/O operations on a JSON file ?

I have the TBufferJSON class working, but not sure which methods to use, for example :

`> ExportToFile (const char *filename, const TObject *obj, const char *option=0)`

but, not sure, if an equivalent i.e

> TBufferJSON::LoadFromFile(fileName)

is available. Not sure where to get started.

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [September 12, 2017, 9:57pm UTC](https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198/2 "2017-09-12T21:57:17Z")

</div>

I don’t think deserializing from a JSON file is supported by `TBufferJSON`. (yet?)

Most (if not all) of the read-related methods seem to be no-op:

- [https://github.com/root-project/root/blob/master/io/io/src/TBufferJSON.cxx#L2809](https://github.com/root-project/root/blob/master/io/io/src/TBufferJSON.cxx#L2809)

Interestingly, `TBufferXML` does provide what you want:

- [https://github.com/root-project/root/blob/master/io/xml/inc/TBufferXML.h#L47](https://github.com/root-project/root/blob/master/io/xml/inc/TBufferXML.h#L47)

so… either “just” implement `TBufferJSON::ConvertFromJSON` (drawing some inspiration from its XML counterpart) or have a little json-\>xml converter on the side and use `TBufferXML::ConvertFromXML`.

---

<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:** [September 13, 2017, 7:26am UTC](https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198/3 "2017-09-13T07:26:28Z")

</div>

> [@hep\_physics](#):
>
> Does root have the provision to do I/O operations on a JSON file ?

A canvas can be saved as a JSON file. May be @linev can comment about it.

---

<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:** [September 13, 2017, 7:41am UTC](https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198/4 "2017-09-13T07:41:21Z")

</div>

Hi,

For the moment TBufferJSON is only capable to produce JSON representation for the objects.  
Reading back of JSON data is not implemented.  
Probably, you could try TBufferXML class.  
Produced XML structures can be read back.

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![hep\_physics](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/53a042/32.png) [@hep\_physics](https://root-forum.cern.ch/u/hep_physics)\
**Post date:** [September 13, 2017, 3:18pm UTC](https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198/5 "2017-09-13T15:18:59Z")

</div>

I was able to generate a JSON for the TObject parameters from TCanvas.

In order to use an external json library, probably something minimal that I am going to choose, how can I compile with root ?

i.e.

**root -l “some options to load external file” myMacro.C**

---

<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:** [September 13, 2017, 4:45pm UTC](https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198/6 "2017-09-13T16:45:46Z")

</div>

Hi,

> I was able to generate a JSON for the TObject parameters from TCanvas.

This is normal functionality of TBufferJSON - producing JSON string from arbitrary C++ object.

> In order to use an external json librar

That kind of library you mean?  
If you have any kind of JavaScript engine, just call:

```
var obj = JSON.parse(json_string);

```

For a complex objects, which has multiple cross-references, special function provided in JSROOT.

But again - only one-way conversion is possible with TBufferJSON:

C++ object —\> JSON string —\> JavaScript object

It is not possible to use JSON string and create same kind of C++ object again.

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![binet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/binet/32/11089_2.png) [@binet](https://root-forum.cern.ch/u/binet)\
**Post date:** [September 13, 2017, 5:51pm UTC](https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198/7 "2017-09-13T17:51:52Z")

</div>

I guess, now, the question is to know what the OP wants to achieve… Just another xy problem 🙂

[http://xyproblem.info](http://xyproblem.info)

---

<div class="post-metadata">

**Author:** ![hep\_physics](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/53a042/32.png) [@hep\_physics](https://root-forum.cern.ch/u/hep_physics)\
**Post date:** [September 13, 2017, 6:34pm UTC](https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198/8 "2017-09-13T18:34:43Z")

</div>

External library for example :

> **[nlohmann/json](https://github.com/nlohmann/json)**
>
> JSON for Modern C++. Contribute to nlohmann/json development by creating an account on GitHub.

  
, which is C++ based so that I can use JSON + ROOT ?

I will be generating a JSON object from inside the program. Any other way other than using JSROOT. Since I work on Linux.

TBufferJSON is converting the ROOT based objects like TH1D, into json objects.

---

<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:** [September 13, 2017, 6:50pm UTC](https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198/9 "2017-09-13T18:50:26Z")

</div>

Hi,

You are free to use any kind of library to read JSON data back into your program.  
I see no problem to use library you mention - it should work with created JSON files.  
But very next question - that you want to do with such data?

You took TH1 as example.  
It has many internal attributes, 3x TAxis objects and array of bins at the end.  
Like this one:

[https://root.cern/js/files/th2ul.json.gz](https://root.cern/js/files/th2ul.json.gz)

That you want to use out of it? And how?

JSROOT provides a way to create graphics out of such data.  
Either in normal web browser or in Node.js environment.

[https://root.cern/js/latest/?json=https://root.cern/js/files/th2ul.json.gz&opt=col](https://root.cern/js/latest/?json=https://root.cern/js/files/th2ul.json.gz&opt=col)

SVG file, created with Node.js:

> <https://github.com/linev/jsroot-test/blob/master/TH2/col.svg>

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![hep\_physics](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/53a042/32.png) [@hep\_physics](https://root-forum.cern.ch/u/hep_physics)\
**Post date:** [September 13, 2017, 7:28pm UTC](https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198/10 "2017-09-13T19:28:22Z")

</div>

I am not quite familiar with Node. Was looking up some C++ json libraries for now and use it with root. So my question was, if I have a library compiled, then to link up to g++ say, I would d something like

**g++ -I\path\to\[someHeader.sh](http://someHeader.sh)[myProgram.cc](http://myProgram.cc)**

I guess root also have a provision to include other libraries, when I do the command, not familiar with the switches.

**root someFlagsForExtLibrary myProgram.C**

do you have some examples or a page for more information on how to do it ?

---

<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:** [September 13, 2017, 7:38pm UTC](https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198/11 "2017-09-13T19:38:40Z")

</div>

Hi,

Before we come to very basics of C++ and ROOT -  
can you describe in several sentences that you want to do beside reading JSON with some external library.

---

<div class="post-metadata">

**Author:** ![hep\_physics](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/53a042/32.png) [@hep\_physics](https://root-forum.cern.ch/u/hep_physics)\
**Post date:** [September 13, 2017, 7:54pm UTC](https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198/12 "2017-09-13T19:54:42Z")

</div>

I have a lot of files, basically trees and histograms that I read and do some division operations. I would like to have a json file so that I can export it into my program after the processing is completed and apply it to similar files. I want to operate on a single file, generate the options in JSON file and read it and use these options for the other files, which are quite similar.

A simple JSON Schema :  
{“fileName1”:[“a”, “b”, “c”], “fileName2”:[“p”, “q”, 'r"]}

with a JSON library, I hope I can append new objects easily and do the basic stuff as the files are processed by the macro.

---

<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:** [September 13, 2017, 8:07pm UTC](https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198/13 "2017-09-13T20:07:42Z")

</div>

Probably, XML format is more appropriate for you.

ROOT provides XML parser and all infrastructure to create, parse, modify arbitrary XML files.  
Look to TXMLParser class - it uses libxml2 behind.  
Or as alternative - more simple TXMLEngine class (I wrote it approx 12 years ago)

You can check in root tutorials/xml/ subfolder for examples.  
Like xmlreadfile.C or xmlnewfile.C

TBufferJSON developed for different purposes and does not allow to implement directly that you want.

---

<div class="post-metadata">

**Author:** ![hep\_physics](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/53a042/32.png) [@hep\_physics](https://root-forum.cern.ch/u/hep_physics)\
**Post date:** [September 13, 2017, 8:35pm UTC](https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198/14 "2017-09-13T20:35:43Z")

</div>

The closest I can get to what I wanted. Thanks @linev !

---

<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:** [September 27, 2017, 8:35pm UTC](https://root-forum.cern.ch/t/i-o-operations-on-json-file/26198/15 "2017-09-27T20:35:43Z")

</div>

Closed on September 27, 2017, 8:35pm UTC

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