Do QJsonDocument::fromJson() changes object position while parsing?
-
Hello Team,
I am creating a small application which should do following:
- Copy existing Json file (say "abc.json") to a new name (say "pqr.json"
- Modify exisintg content of newly created json file "pqr.json".
Issue which I am facing is that in my newly created Json file ("pqr.json".) position of json object changes.
To simplify the problem, I have a created sample json file with less content.
Example:
"abc.json" file is having below contents:
{
"FirstName": "John",
"LastName": "Wick",
"Nick Name": "JW",
"Height": 173
}Modified Json file "pqr.json" changed to:
{
"FirstName": "John",
"Height": 173,
"LastName": "Rambo",
"Nick Name": "JR"
}Here, position of "Height" object is changed from 4th to 2nd. I don't want to change the position of objects because in my original file. If position of the object changes then application will not be able to read/parse data properly.
Below is the code:
bool operationResult = QFile::copy("abc.json", "pqr.json");if(operationResult) { QFile fileToUpdate("pqr.json"); fileToUpdate.open(QIODevice::ReadOnly | QIODevice::Text); QJsonDocument fileToUpdateJsonDocument = QJsonDocument::fromJson(fileToUpdate.readAll()); fileToUpdate.close(); if(!(fileToUpdateJsonDocument.isNull())) { QJsonObject fileToUpdateRootObject = fileToUpdateJsonDocument.object(); if(!fileToUpdateRootObject.isEmpty()) { fileToUpdateRootObject.insert("FirstName", "John"); fileToUpdateRootObject.insert("LastName", "Rambo"); fileToUpdateJsonDocument.setObject(fileToUpdateRootObject); fileToUpdate.open(QFile::WriteOnly | QFile::ExistingOnly); fileToUpdate.write(fileToUpdateJsonDocument.toJson(QJsonDocument::Indented)); fileToUpdate.close(); } } }
It seems to methat : "QJsonDocument::fromJson()" function changes position of the object while parsing.
Please suggest how to solve this issue?
-
Hello Team,
I am creating a small application which should do following:
- Copy existing Json file (say "abc.json") to a new name (say "pqr.json"
- Modify exisintg content of newly created json file "pqr.json".
Issue which I am facing is that in my newly created Json file ("pqr.json".) position of json object changes.
To simplify the problem, I have a created sample json file with less content.
Example:
"abc.json" file is having below contents:
{
"FirstName": "John",
"LastName": "Wick",
"Nick Name": "JW",
"Height": 173
}Modified Json file "pqr.json" changed to:
{
"FirstName": "John",
"Height": 173,
"LastName": "Rambo",
"Nick Name": "JR"
}Here, position of "Height" object is changed from 4th to 2nd. I don't want to change the position of objects because in my original file. If position of the object changes then application will not be able to read/parse data properly.
Below is the code:
bool operationResult = QFile::copy("abc.json", "pqr.json");if(operationResult) { QFile fileToUpdate("pqr.json"); fileToUpdate.open(QIODevice::ReadOnly | QIODevice::Text); QJsonDocument fileToUpdateJsonDocument = QJsonDocument::fromJson(fileToUpdate.readAll()); fileToUpdate.close(); if(!(fileToUpdateJsonDocument.isNull())) { QJsonObject fileToUpdateRootObject = fileToUpdateJsonDocument.object(); if(!fileToUpdateRootObject.isEmpty()) { fileToUpdateRootObject.insert("FirstName", "John"); fileToUpdateRootObject.insert("LastName", "Rambo"); fileToUpdateJsonDocument.setObject(fileToUpdateRootObject); fileToUpdate.open(QFile::WriteOnly | QFile::ExistingOnly); fileToUpdate.write(fileToUpdateJsonDocument.toJson(QJsonDocument::Indented)); fileToUpdate.close(); } } }
It seems to methat : "QJsonDocument::fromJson()" function changes position of the object while parsing.
Please suggest how to solve this issue?
@Learner1985 Welcome to the Qt Forums.
There is no issue. The two data structures are logically identical, the second simply orders the keys alphabetically (probably because the code uses QJsonObject::keys()). The order of keys in a JSON object is not specified in the relevant RFC other than to mention that:
- some parsers may expose the ordering of object members to callers.
- interoperable parsers should not rely on order.
Qt does not expose or preserve the file ordering of object members.
So,
If position of the object changes then application will not be able to read/parse data properly.
Your application parser is fragile and you should focus there.
If order is important then a JSON Array might be a better choice. -
Hello Team,
I am creating a small application which should do following:
- Copy existing Json file (say "abc.json") to a new name (say "pqr.json"
- Modify exisintg content of newly created json file "pqr.json".
Issue which I am facing is that in my newly created Json file ("pqr.json".) position of json object changes.
To simplify the problem, I have a created sample json file with less content.
Example:
"abc.json" file is having below contents:
{
"FirstName": "John",
"LastName": "Wick",
"Nick Name": "JW",
"Height": 173
}Modified Json file "pqr.json" changed to:
{
"FirstName": "John",
"Height": 173,
"LastName": "Rambo",
"Nick Name": "JR"
}Here, position of "Height" object is changed from 4th to 2nd. I don't want to change the position of objects because in my original file. If position of the object changes then application will not be able to read/parse data properly.
Below is the code:
bool operationResult = QFile::copy("abc.json", "pqr.json");if(operationResult) { QFile fileToUpdate("pqr.json"); fileToUpdate.open(QIODevice::ReadOnly | QIODevice::Text); QJsonDocument fileToUpdateJsonDocument = QJsonDocument::fromJson(fileToUpdate.readAll()); fileToUpdate.close(); if(!(fileToUpdateJsonDocument.isNull())) { QJsonObject fileToUpdateRootObject = fileToUpdateJsonDocument.object(); if(!fileToUpdateRootObject.isEmpty()) { fileToUpdateRootObject.insert("FirstName", "John"); fileToUpdateRootObject.insert("LastName", "Rambo"); fileToUpdateJsonDocument.setObject(fileToUpdateRootObject); fileToUpdate.open(QFile::WriteOnly | QFile::ExistingOnly); fileToUpdate.write(fileToUpdateJsonDocument.toJson(QJsonDocument::Indented)); fileToUpdate.close(); } } }
It seems to methat : "QJsonDocument::fromJson()" function changes position of the object while parsing.
Please suggest how to solve this issue?
@Learner1985 said in Do QJsonDocument::fromJson() changes object position while parsing?:
Here, position of "Height" object is changed from 4th to 2nd. I don't want to change the position of objects because in my original file. If position of the object changes then application will not be able to read/parse data properly.
Then your app is doing something wrong. There is no defined order for json object values.