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!
-
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".
-
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).
-
-
@rosh7 said in JSON object iteration and comparision:
I don't know what
d
ora
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 noQJsonValue
constructor like you are trying to use. I don't think you will want to be creatingQJsonValue
s, you're interested in whatever values you already have.
The intention looks like it would iterate through someQJsonArray
values.
When theQJsonValue
s differ, for your "project" I imagine you are expected to do something about looking at what type the values are, if they areQJsonObject
I imagine you would be expected to recurse to see what differsmy object contains arrays, strings, int and double encapsulated in a nested object format.
The "nested" bit.
There is an existingbool QJsonArray::operator==(const QJsonArray &other) const
, https://doc.qt.io/qt-5/qjsonarray.html#operator-eq-eq. -
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????
-
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.