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

JSON object iteration and comparision



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


  • Lifetime Qt Champion

    Hi and welcome to devnet,

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



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



  • This post is deleted!

  • Lifetime Qt Champion

    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.



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



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



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



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



  • This post is deleted!

  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    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.


Log in to reply