Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get nested json values
QtWS25 Last Chance

Get nested json values

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 3 Posters 4.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    You have to unpack them by yourself.

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

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    5
    • A Offline
      A Offline
      avmg
      wrote on last edited by
      #3

      Thanks Christian!

      JonBJ 1 Reply Last reply
      0
      • A avmg

        Thanks Christian!

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #4

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

        A 1 Reply Last reply
        0
        • JonBJ JonB

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

          A Offline
          A Offline
          avmg
          wrote on last edited by
          #5

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

          JonBJ 1 Reply Last reply
          0
          • A avmg

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #6

            @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;
            
            A 1 Reply Last reply
            1
            • JonBJ JonB

              @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;
              
              A Offline
              A Offline
              avmg
              wrote on last edited by
              #7

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

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

              ..when debuging it shows "Null"
              ¿?

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @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;
                

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                A 1 Reply Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  @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;
                  
                  A Offline
                  A Offline
                  avmg
                  wrote on last edited by
                  #9

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

                  JonBJ 1 Reply Last reply
                  0
                  • A avmg

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #10

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

                    A 1 Reply Last reply
                    0
                    • JonBJ JonB

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

                      A Offline
                      A Offline
                      avmg
                      wrote on last edited by
                      #11

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

                      JonBJ 1 Reply Last reply
                      0
                      • A avmg

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

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #12

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

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          avmg
                          wrote on last edited by
                          #13

                          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!

                          JonBJ 1 Reply Last reply
                          0
                          • A avmg

                            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!

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #14

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

                            A 1 Reply Last reply
                            0
                            • JonBJ JonB

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

                              A Offline
                              A Offline
                              avmg
                              wrote on last edited by
                              #15

                              @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!

                              JonBJ 1 Reply Last reply
                              0
                              • A avmg

                                @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!

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #16

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

                                1 Reply Last reply
                                1
                                • A Offline
                                  A Offline
                                  avmg
                                  wrote on last edited by avmg
                                  #17

                                  Hi again!

                                  I'm trying to do what we were talking about but I'm fighting with the same thing but opposite...

                                  How can I nest a value inside another in a json? I've tried things like:

                                  json["Food"].toObject()["Fruits"].toObject()["Apples"].toDouble(5);
                                  

                                  and

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

                                  It doesn't generate errors but the file shows equal to null after Food... I imagine that firstly I should create another QJsonObject and insert it in another, but I've tried many things without success...

                                  Any suggestion?
                                  Thanks in advance!

                                  1 Reply Last reply
                                  0
                                  • Christian EhrlicherC Offline
                                    Christian EhrlicherC Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on last edited by Christian Ehrlicher
                                    #18

                                    JonB already showed you how to do it: https://forum.qt.io/topic/116865/get-nested-json-values/13

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    A 1 Reply Last reply
                                    1
                                    • Christian EhrlicherC Christian Ehrlicher

                                      JonB already showed you how to do it: https://forum.qt.io/topic/116865/get-nested-json-values/13

                                      A Offline
                                      A Offline
                                      avmg
                                      wrote on last edited by
                                      #19

                                      @Christian-Ehrlicher Hi Christian,

                                      You are right, I was looking the line:

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

                                      ...but the solution was at the beginning just in front of me:

                                      QJsonObject json, food, fruits;
                                      fruits["Apples"] = 10;
                                      food["Fruits"] = fruits;
                                      json["Food"] = food;
                                      
                                      // next line works right
                                      qDebug() << json["Food"].toObject()["Fruits"].toObject()["Apples"];
                                      

                                      Thanks again and sorry, I didn't pay attention.

                                      Cheers

                                      JonBJ 1 Reply Last reply
                                      0
                                      • A avmg

                                        @Christian-Ehrlicher Hi Christian,

                                        You are right, I was looking the line:

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

                                        ...but the solution was at the beginning just in front of me:

                                        QJsonObject json, food, fruits;
                                        fruits["Apples"] = 10;
                                        food["Fruits"] = fruits;
                                        json["Food"] = food;
                                        
                                        // next line works right
                                        qDebug() << json["Food"].toObject()["Fruits"].toObject()["Apples"];
                                        

                                        Thanks again and sorry, I didn't pay attention.

                                        Cheers

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on last edited by
                                        #20

                                        @avmg
                                        Yes.

                                        Note that we are saying you can read nested JSON objects via

                                        qDebug() << json["Food"].toObject()["Fruits"].toObject()["Apples"];
                                        

                                        but you cannot write the value of a nested object via

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

                                        That's just how it is, as the code above copies the JSON objects, so assigning does not change what is in json[].... If you want to achieve the second example you have to build it up bit by bit as per the 4 lines

                                        QJsonObject json, food, fruits;
                                        fruits["Apples"] = 10;
                                        food["Fruits"] = fruits;
                                        json["Food"] = food;
                                        
                                        1 Reply Last reply
                                        0

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved