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. JSON object iteration and comparision
Forum Update on Tuesday, May 27th 2025

JSON object iteration and comparision

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 2.2k Views 2 Watching
  • 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.
  • R Offline
    R Offline
    rosh7
    wrote on 3 Apr 2020, 18:22 last edited by rosh7 4 Mar 2020, 18:30
    #1

    Hey guys!

    I'm pretty much new to QT and an amateur at C++ programming. I have a task where I must compare two JSON objects and I'm kinda confused as to how to approach this. Under documentation, I did see a method for iteration but I am confused as to how will I do it for two objects within the same loop simultaneously.

    I want to compare each value individually inside my test object with my reference object and display whether or not they are the same.

    my object contains arrays, strings, int and double encapsulated in a nested object format.

    A basic logic as to how to approach this would be really helpful.

    Again, I'm an amateur and I'm using this as a learning experience to get a feel of working in QT

    Thanks in advance!

    J 1 Reply Last reply 3 Apr 2020, 18:30
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 3 Apr 2020, 18:26 last edited by
      #2

      Hi and welcome to devnet,

      If you just want to verify that they are equal you can use the QJsonDocument equality operator.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • R rosh7
        3 Apr 2020, 18:22

        Hey guys!

        I'm pretty much new to QT and an amateur at C++ programming. I have a task where I must compare two JSON objects and I'm kinda confused as to how to approach this. Under documentation, I did see a method for iteration but I am confused as to how will I do it for two objects within the same loop simultaneously.

        I want to compare each value individually inside my test object with my reference object and display whether or not they are the same.

        my object contains arrays, strings, int and double encapsulated in a nested object format.

        A basic logic as to how to approach this would be really helpful.

        Again, I'm an amateur and I'm using this as a learning experience to get a feel of working in QT

        Thanks in advance!

        J Offline
        J Offline
        JonB
        wrote on 3 Apr 2020, 18:30 last edited by JonB 4 Mar 2020, 18:30
        #3

        @rosh7 said in JSON object iteration and comparision:

        compare two JSON objects

        If you mean to see if they are the same it's @SGaist's one-liner!

        I was thinking you meant show the user visually how they compare, which is a whole different ballgame, but might be what you mean by "as a learning experience to get a feel of working in QT".

        1 Reply Last reply
        1
        • R Offline
          R Offline
          rosh7
          wrote on 3 Apr 2020, 18:32 last edited by rosh7 4 Mar 2020, 18:36
          #4
          This post is deleted!
          J 1 Reply Last reply 3 Apr 2020, 18:39
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 3 Apr 2020, 18:39 last edited by
            #5

            You then might want to consider nlohmann/json which provides a diff method that will provide you with a patch containing the differences between two JSON which might be easier to use for your purpose. You can then parse the result to build your GUI content.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            R 1 Reply Last reply 3 Apr 2020, 19:02
            2
            • R rosh7
              3 Apr 2020, 18:32

              This post is deleted!

              J Offline
              J Offline
              JonB
              wrote on 3 Apr 2020, 18:39 last edited by JonB 4 Mar 2020, 18:42
              #6

              @rosh7
              In a generic sense this is a non-trivial task. If you think about, if your two objects are very different (or not so very different) you will soon have no idea where the "corresponding" node might be between the two objects.

              That said, for a simple approach: I think you understand that you will iterate across sibling nodes and recurse into child nodes. Essentially you will have a marker for where you have got to in your reference object and another marker for where you have got to your comparison object. As you advance through the reference one you will move on the marker in the compared one.

              If you are a beginner, this is not the simplest task you could have picked.

              EDIT I have just seen what @SGaist posted. That is obviously a quite different approach. It depends what you want to code/as result.

              1 Reply Last reply
              1
              • R Offline
                R Offline
                rosh7
                wrote on 3 Apr 2020, 18:46 last edited by
                #7

                @SGaist @JonB
                The order of my objects, the length and the strings inside my both objects are the same. The float and integer values might be slightly off and that is what I wish to display.

                I can't use nlohmann json because my supervisor didn't allow me to use it in their project.

                Hence I must write a code to iterate through the objects and display the differences.

                I need help with the syntax of iterating two objects (which is confusing to me).

                1 Reply Last reply
                0
                • S SGaist
                  3 Apr 2020, 18:39

                  You then might want to consider nlohmann/json which provides a diff method that will provide you with a patch containing the differences between two JSON which might be easier to use for your purpose. You can then parse the result to build your GUI content.

                  R Offline
                  R Offline
                  rosh7
                  wrote on 3 Apr 2020, 19:02 last edited by
                  #8

                  @SGaist @JonB

                  bool QJsonArray::operator==(const QJsonArray &other) const
                  {
                  for (int i = 0; i < (int)a->length; ++i) {
                  if (QJsonValue(d, a, a->at(i)) != QJsonValue(other.d, other.a, other.a->at(i)))
                  return false;
                  }
                  return true;
                  }

                  Will this iterate through every single value in my object?

                  J 1 Reply Last reply 3 Apr 2020, 19:34
                  0
                  • R rosh7
                    3 Apr 2020, 19:02

                    @SGaist @JonB

                    bool QJsonArray::operator==(const QJsonArray &other) const
                    {
                    for (int i = 0; i < (int)a->length; ++i) {
                    if (QJsonValue(d, a, a->at(i)) != QJsonValue(other.d, other.a, other.a->at(i)))
                    return false;
                    }
                    return true;
                    }

                    Will this iterate through every single value in my object?

                    J Offline
                    J Offline
                    JonB
                    wrote on 3 Apr 2020, 19:34 last edited by JonB 4 Mar 2020, 19:45
                    #9

                    @rosh7 said in JSON object iteration and comparision:

                    I don't know what d or a are. And they're not members of QJsonArray (that I know of).
                    int QJsonArray::size() const returns the number of values stored in the array.
                    There is no QJsonValue constructor like you are trying to use. I don't think you will want to be creating QJsonValues, you're interested in whatever values you already have.
                    The intention looks like it would iterate through some QJsonArray values.
                    When the QJsonValues differ, for your "project" I imagine you are expected to do something about looking at what type the values are, if they are QJsonObject I imagine you would be expected to recurse to see what differs

                    my object contains arrays, strings, int and double encapsulated in a nested object format.

                    The "nested" bit.
                    There is an existing bool QJsonArray::operator==(const QJsonArray &other) const, https://doc.qt.io/qt-5/qjsonarray.html#operator-eq-eq.

                    R 1 Reply Last reply 3 Apr 2020, 19:45
                    0
                    • J JonB
                      3 Apr 2020, 19:34

                      @rosh7 said in JSON object iteration and comparision:

                      I don't know what d or a are. And they're not members of QJsonArray (that I know of).
                      int QJsonArray::size() const returns the number of values stored in the array.
                      There is no QJsonValue constructor like you are trying to use. I don't think you will want to be creating QJsonValues, you're interested in whatever values you already have.
                      The intention looks like it would iterate through some QJsonArray values.
                      When the QJsonValues differ, for your "project" I imagine you are expected to do something about looking at what type the values are, if they are QJsonObject I imagine you would be expected to recurse to see what differs

                      my object contains arrays, strings, int and double encapsulated in a nested object format.

                      The "nested" bit.
                      There is an existing bool QJsonArray::operator==(const QJsonArray &other) const, https://doc.qt.io/qt-5/qjsonarray.html#operator-eq-eq.

                      R Offline
                      R Offline
                      rosh7
                      wrote on 3 Apr 2020, 19:45 last edited by rosh7 4 Mar 2020, 19:47
                      #10
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 3 Apr 2020, 20:12 last edited by
                        #11

                        You use the keys of the json data you are iterating to extract the corresponding values of both json. Then you compare these values.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        R 1 Reply Last reply 3 Apr 2020, 20:26
                        1
                        • S SGaist
                          3 Apr 2020, 20:12

                          You use the keys of the json data you are iterating to extract the corresponding values of both json. Then you compare these values.

                          R Offline
                          R Offline
                          rosh7
                          wrote on 3 Apr 2020, 20:26 last edited by rosh7 4 Apr 2020, 08:40
                          #12
                             if(bool(refobject.length()==testobject.length())==true)
                             {
                              QJsonObject::iterator i,j;
                              for (i = refobject.begin(), j= testobject.begin(); 
                                            i!=refobject.end()&&j!=testobject.end(); ++i,++j)
                              {
                              if(i.key()==j.key())
                                 {     
                                 if(i.value().isString() && j.value().isString())
                                 { 
                                     if(i.value()==j.value())
                                     {
                                       //display i and j
                                     }
                                     else 
                                     {
                                      //display them in a different colour 
                                     }
                                     
                                   }
                                 else if(i.value().isArray()&& j.value().isArray())
                                 {
                                   //iterate and compare
                                 }
                                 else if(i.value().isObject() && j.value().isObject())
                                 {
                                    //check if there are strings or integers or array and work accordingly 
                                 }
                                 else if(i.value().isDouble() && j.value().isDouble())
                                 {
                                     if(i.value()==j.value())
                                     {
                                       //display i and j
                                     }
                                     else 
                                     {
                                        //display them in a different colour 
                                     }
                                     
                                 }
                          
                              }
                          }
                          }
                          
                          else{//display message that you cannot compare them}
                          

                          }
                          }

                          This is a rough code I wrote for this Logic. Can I work on this idea????

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 4 Apr 2020, 20:08 last edited by
                            #13

                            I would rather iterate through one json object and retrieve the corresponding data from the second as your documents may be stored with keys in a different order.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            2

                            1/13

                            3 Apr 2020, 18:22

                            • Login

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