Saving songs in JSON

I am a newbie an building an app where I need to save songs (title,track time and storage path) in a JSON file. The problem is that I can’t figure out how to read and write in JSON. I saw this post:

But when I want to read the file like so:
File myFile= File::getCurrentWorkingDirectory().getChildFile(“file.json”);
auto saved = JSON::parse(myFile);
DBG("result: "+saved.toString());

It’s all blank.

Same when I want to write into the file:

FileOutputStream output(myFile);
auto stringified = JSON::toString(“save this text”);
JSON::writeToStream(outputStream, stringified);

This doesn’t work for me either, nothing gets written, nothing gets read :frowning:

File::getCurrentWorkingDirectory() might not return the directory that you expect, the working directory can differ depending on how the application was started, so it’s usually not recommended for any portable solution. Did you make sure that you are not accidentally dealing with the wrong or a non existing file?

1 Like

As mentioned, if you’re not seeing any output at all, then you should double-check to make sure myFile actually exists:

jassert (myFile.existsAsFile());

That said, there are a few errors in your example code regarding how juce::var works with JSON objects.

File myFile= File::getCurrentWorkingDirectory().getChildFile(“file.json”);
auto saved = JSON::parse(myFile);
DBG("result: "+ saved.toString()); // <-- error here

Calling saved.toString() isn’t what you want here, you should use JSON::toString() instead:

DBG("result: " + JSON::toString (saved));

And when writing the data:

FileOutputStream output(myFile);
auto stringified = JSON::toString(“save this text”); // <-- error here
JSON::writeToStream(outputStream, stringified);

You call JSON::toString() with a string literal that does not contain JSON data. Instead you should build the JSON structure using DynamicObject, then create a juce::var from that object which you can then write to the file:

auto obj = std::make_unique<DynamicObject>();
obj->setProperty ("prop1", "value1");

auto jsonVar = juce::var (obj.release());

FileOutputStream output (myFile);
JSON::writeToStream (output, jsonVar);

Hope this helps!

1 Like

Thanks for your answer :smiley: . I can confirm that the existsAsFiles returns true but I can’t read from it. I noticed in the Visual Studio debugger that the following JSONParser exception gets shown:

The JSON file I am using is the example JSON file found here: JSON Example so it’s not a formatting issue. I literally copy-pasted that online example from that website and saved it in myStorage.json which shows no syntax mistakes when I open it on VSCode either.

I really don’t know how to debug this JSONParser error.

VS debug output:

exists as file
Exception thrown at 0x00007FFA8AB94F69 in myJuceProject.exe: Microsoft C++ exception: juce::JSONParser::ErrorException at memory location 0x000000BAF0B0E6A8.
result: null

My code inside myJuceProject/Source/customComponent.cpp

File myFile = File::getCurrentWorkingDirectory().getChildFile("myStorage.json");        
        if (myFile.existsAsFile()) {
            DBG("exists as file");
        }
        else {
            DBG("does not exist as file");
        }
        //jassert(myFile.existsAsFile());        
        auto saved = JSON::parse(myFile);
        DBG("result: " + JSON::toString(saved));

UPDATE: I figured it out. Thanks, both @connorreviere, and @PluginPenguin . I incorrectly assumed it was reading the file from the same directory where I had my .cpp file (myJuceProject/Source folder created by PROJUCER) but it turns out it was reading and writing in a file with the same name in myJuceProject/Builds/VisualStudio/2022.
And the JSONParse reading error was correct because that other file (myJuceProject/Builds/VisualStudio/2022) did have syntax mistakes.
Thanks once again!