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 Updated to NodeBB v4.3 + New Features

JSON object iteration and comparision

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 2.3k 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 last edited by rosh7
    #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!

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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

        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!

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #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 last edited by rosh7
          #4
          This post is deleted!
          JonBJ 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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
            2
            • R rosh7

              This post is deleted!

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #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 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
                • SGaistS SGaist

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

                  JonBJ 1 Reply Last reply
                  0
                  • R rosh7

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

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by JonB
                    #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
                    0
                    • JonBJ JonB

                      @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 last edited by rosh7
                      #10
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 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
                        1
                        • SGaistS SGaist

                          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 last edited by rosh7
                          #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
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 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

                            • Login

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