Loading json from string?

Kind of stuck on this atm, how can I load an ofJson object from a string that contains json data. There only seems to be a function to load from a file in the openframeworks ofJson.h file?

string data = "{\"bboxes\":[[0.10875897854566574,0.16948606073856354,0.9021437764167786,0.9771744012832642]],\"classes\":[\"person\"],\"scores\":[0.9999969005584717]}";
    
    ofJson jsonData = data;

Tried this and it just crashes when I try to access anything in the jsonData object.

Hi, ofJson is simply a typedef (a name alias) for nlohmann::json which has a really thorough documentation. Thus, if you are stuck at some point using ofJson, just google for nlohmann::json instead and you’ll probably find a lot of info.

As for the specific issue you have
you can use the parse function as follows

ofJson j = ofJson::parse("{\"bboxes\":[[0.10875897854566574,0.16948606073856354,0.9021437764167786,0.9771744012832642]],\"classes\":[\"person\"],\"scores\":[0.9999969005584717]}");

This would be exactly the same as calling

ofJson j = nlohmann::json::parse("{\"bboxes\":[[0.10875897854566574,0.16948606073856354,0.9021437764167786,0.9771744012832642]],\"classes\":[\"person\"],\"scores\":[0.9999969005584717]}");

Then, from the string you are trying to parse I think it comes from DenseCap. If I am correct, take a look how this is handled in ofxRunway (which I wrote btw).
Best

1 Like

thanks that makes sense. Should have dug a little deeper.

And yes it is for ofxRunway, which I’m having an additional problem with, the /server/connect osc message doesn’t trigger Runway to start streaming messages? If I send the same message from processing or any other app it triggers Runway fine. I’ve checked with an osc monitor app and the message sent from OF looks the same. Just wondering if you have seen a similar problem?

Hi, dont use it with osc, ofxRunway does not have it implemented. RunwayML’s implementation of OSC is a bit odd, as it expects you to send a sort of handshake message before it starts sending anything. Also it is simply sending a string with the JSON in it, making it a bit senseless besides using plain HTTP. Thus, use HTTP instead of OSC.