I/O operations on JSON file

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


is available. Not sure where to get started.

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:

Interestingly, TBufferXML does provide what you want:

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.

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


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.


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 ?


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


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.


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


External library for example :

, 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.


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:


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.


SVG file, created with Node.js:


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 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 ?


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.

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.

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.

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

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