Saving json files

Hello!

I made a plugin that measures the input signal and outputs the value every second on the GUI. I would like to save every 5 of these values (basically each 5 seconds I save) and the name of the channel they are coming from, in different json files.

What would be the approach?

Thanks!

General note, under docs.juce.com you find an overview of all classes JUCE provides. It’s always a good idea to search for a class that seems to be made for what you plan here. There you’ll find the JSON class, which is obviously what you need – at least if you try to create json data with JUCE onboard tools.

The JUCE JSON class takes a var type and converts it to json. However, if you need some nested structure to pass to your json output, you will need to construct it using the DynamicObject class, encapsulate that into a var and convert this to json. This is not so intuitive and straightforward as one could think…

Another option, resulting in much more readable code is the famous JSON for Modern C++ library. It’s header only and the git repo contains a lot of example code. However, you add another dependency to your project which I personally wouldn’t prefer if the jsons to create would be fairly simple and creating jsons would not be a major part of the code.

2 Likes

Thank you so much!

Well. I already saved my data in json format, but i wanted to save it to a json file.

auto jsonString = JSON::toString (x);

Is there any example for saving json data to a json file?

Something like…

File myFile ("test.json");
myFile.replaceWithText (jsonString);
1 Like

Another option would be JSON::writeToStream? You can pass in a FileOutputStream instance that refers to the file you want to write to.

2 Likes