Solved QJsonDocument: Insert/update new QJsonObject
-
Hi, I would like to know how can I insert (or update) a new
QJsonObject
in theQJsonDocument
that comes from a JSON file.
Example:// Open a JSON file from disk: QFile file(_data_file_path); if(!file.open(QIODevice::ReadWrite)){ qDebug() << "Failed to open " << file.fileName(); exit(1); } // Get Json byte array: QTextStream file_text(&file); QString json_string = file_text.readAll(); QByteArray json_bytes = json_string.toLocal8Bit(); // Get QJsonDocument: auto doc = QJsonDocument::fromJson(json_bytes); // Error checking: if(doc.isNull()) { qFatal("Failed to create JSON doc"); exit(2); } // Error checking: if(!doc.isObject()) { qFatal("JSON is not an object"); exit(3); } // Create a new QJsonObject to insert/update in a QJsonDocument: QJsonObject new; new.insert("new field 1", 1); new.insert("new field 2", 2); // ===== HERE IS THE PROBLEM ===== // // Insert/update the new object in the QJsonDocument // Question 1: How can I do this? // Question 2: And if I already have a specific JSON field in my file, how can I update just this specific values? // After write the modifications, close the updated file: file.close();
I'm asking because
QJsonDocument
doesn't have theinsert()
method.
I tried something like this:// Convert QJsonDocument to QJsonObject QJsonObject doc_obj = doc.object(); // Insert the new object: doc_obj.insert("my_new_data", new); // Create another QJsonDocument to hold the updated QJsonDocument QJsonDocument new_doc(doc_obj); // Clear current file: file.resize(0); // Write to file: file.write(new_doc.toJson()); file.close();
It works, but I have to clear all my file and always overwrite it.
I would like to indicate: "Just update this field" or "Just append a new QJsonObject in the current JSON file"Could you help me?
-
@fem_dev
With the proviso that I have not used JSON stuff from Qt...(I believe) You are indeed not meant to "just update things in a JSON file". Think of JSON as a serialized format for an object(s). To manipulate/alter, you deserialize it into memory, change the in-memory object, then serialize the whole thing back to file, Your attempt is therefore along the right lines.
Have a read of https://www.qtcentre.org/threads/65635-QJsonDocument-How-to-replace-a-single-object-or-add-it-if-it-doesn-t-exists?p=289328#post289328 for the overview. Also https://stackoverflow.com/questions/25411339/how-i-can-add-more-than-one-qjsonobject-to-a-qjsondocument
-
@jonb you are right! Thanks!
-
@jonb just to confirm:
So, it's not possible in any way to just append new JSON Objects in a *.json file without load all file data in memory and manipulate it on memory?
Its not possible to use some kind of "JSON Pointer" that is pointed to some JSON object/array inside the JSON file and write a new content directly there? Without load it in memory....
-
@fem_dev A file is nothing fancy it's just a container of a bunch of bytes. Files do not have "insert" functionalities. So no, it's not possible