Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Get nested json values



  • Hi all,

    I'm trying to get the numeric and nested values from a QJsonObject. In the watch window I can see for example:

    ->json
    -->"Food"
    --->"Fruits"
    ---->"Apples: 5"
    --->"Groups: 2"

    It successfully gets the value from "Groups" like:
    json["Food.Groups"].toDouble();

    But for the case of "Apples" that is nested inside Food and fruits it doesn't work like:
    json["Food.Fruits.Apples"].toDouble();

    ...it returns zero.

    I have seen other solutions but sure there's something really simple that I might be doing wrong, Isn't it?

    Thanks in advance!


  • Qt Champions 2019

    You have to unpack them by yourself.

    QJsonObject food = json["food"].toObject();
    QJson fruits = food["Fruits"].toObject()
    ...
    


  • Thanks Christian!



  • @avmg
    I would do it explictly like @Christian-Ehrlicher shows.

    What remains a mystery is your report:

    It successfully gets the value from "Groups" like:
    json["Food.Groups"].toDouble();

    But for the case of "Apples" that is nested inside Food and fruits it doesn't work like:
    json["Food.Fruits.Apples"].toDouble();

    I didn't know if/whether/that JSON accepts key1.key2 notation at all to access sub-objects, but if it does I can't see why Food.Groups would work OK but Food.Fruits.Apples would not? Are you sure the former works while the latter fails?? Where does it say key1.key2 (Food.Groups) works at all, please?



  • @JonB key1.key2 successfully works but key1.key2.key3 it doesn't I don't know why.... I just made it like Christian and it works.

    My next question is, How about the opossite? How to write instead of read in the same example?



  • @avmg
    You just do the opposite! Where you had

    QJsonObject food = json["food"].toObject();
    

    you might have:

    QJsonObject food;
    # put things into `food`
    json["food"] = food;
    


  • @JonB but how to for example update the "Apples" value?

    json["Food"].toObject()["Fruits"]toObject()["Apples"] = 10;

    ..when debuging it shows "Null"
    ¿?


  • Qt Champions 2019

    @avmg said in Get nested json values:

    ..when debuging it shows "Null"

    Because it does return new objects. You have to set it again back with e.g.

    auto obj = json["Food"].toObject();
    // do something with obj
    json["Food"] = obj;
    


  • @Christian-Ehrlicher sorry but I don't see it...

    I have a mix of things that I don't understand, I just see different codes on each answer...

    In order to use json it must be declared previously, so I don't understand what are you doing. My target is to change the value of a nested json, I tried like:

    QJsonObject json;
    json["Food"].toObject()["Fruits"]toObject()["Apples"] = 10;

    I found a lot of info and hundred of ways to do the things with json files and I think it's just creating more confusion to me...



  • @avmg
    If you do that, your empty QJsonObject json won't even have a Food sub-object in it. You need to put stuff into your object.

    Untested, but you will want something like:

    QJsonObject json, food, fruits;
    json["Food"] = food;
    food["Fruits"] = fruits;
    fruits["Apples"] = 10;
    

    EDIT Wrong, see my next post for correction.



  • @JonB and is it possible to nest again?

    The target of all of this is to save the values from a numeric struct into a json file, and show it in a treeview, I like the way how the json is organized because when you load it into the treeview it results very clean and organized for the user. Actually I can load the json and update the struct with its values, but when I modify the struct and I want to save it back into the json file is when I struggle... that's why i'm asking how to modify one specific value from a nested json, the json already exists, but I can't find the way to change it.



  • @avmg
    I'm sorry, I don't have time right now to complete looking into this.

    I can say that my example for setting the values needs the order of its assignments reversed:

    QJsonObject json, food, fruits;
    fruits["Apples"] = 10;
    food["Fruits"] = fruits;
    json["Food"] = food;
    
    // next line works right
    qDebug() << json["Food"].toObject()["Fruits"].toObject()["Apples"];
    
    // assignment not right here, it must be into into a copy of objects
    json["Food"].toObject()["Fruits"].toObject()["Apples"] = 999;
    // next line wrong, still prints `10`
    qDebug() << json["Food"].toObject()["Fruits"].toObject()["Apples"];
    

    Somewhere the assignment at the end needs to use QJsonValueRefs instead of those toObject()s which I think are doing a copy. I may have time to look at it later, but have a play....



  • Hi JonB,

    That's exactly what I was trying and the point where I got stacked, there's no error but the value isn't assigned... thanks anyway!



  • @avmg
    I too have had little joy. In the end I searched, and you should read through (all of) https://stackoverflow.com/questions/17034336/how-to-change-qjsonobject-value-in-a-qjson-hierarchy-without-using-copies carefully.

    The gist being, you can't do it :)

    According to information from Qt developer who actually wrote QJson in Qt5 -

    What's currently included in Qt is a 'read-only' implementation to provide parsing facilities. He has an intention to extend design with 'references' support in future, but it's not yet done.

    After wasting three hours of my life I can confirm that as of today this is still impossible with Qt 5.4. You can modify JSON objects, but not nested JSON objects.

    You can pursue any of the suggestions there to achieve what you want.

    Alternatively, which I would be tempted to do: convert the whole of the JSON object hierarchy into actual C++ structures/QVariantLists/whatever. Do your updates/manipulations on that, as C++/Qt variables instead of on the JSON object tree. And then regenerate the whole JSON document tree from your C++ structures and write that back to file.



  • @JonB
    Ok, so in the same way I'm actually loading the json into the treeview, I should make modifications there and do the opposite operation saving the treeview data into a json file, right?

    Thanks for your time!



  • @avmg
    Yes, exactly that. You produced the treeview internally and output that as JSON. Now, read in the JSON , convert to treeview/internal format, do adds/deletes/updates where you know what you're doing in C++, finally ask for the tree to once again produce the whole of the JSON to save back externally.


Log in to reply