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. QJsonDocument: Insert/update new QJsonObject
Forum Updated to NodeBB v4.3 + New Features

QJsonDocument: Insert/update new QJsonObject

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 3.7k Views
  • 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.
  • F Offline
    F Offline
    fem_dev
    wrote on last edited by
    #1

    Hi, I would like to know how can I insert (or update) a new QJsonObject in the QJsonDocument 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 the insert() 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?

    JonBJ 1 Reply Last reply
    0
    • F fem_dev

      Hi, I would like to know how can I insert (or update) a new QJsonObject in the QJsonDocument 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 the insert() 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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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

      F 2 Replies Last reply
      3
      • JonBJ JonB

        @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

        F Offline
        F Offline
        fem_dev
        wrote on last edited by
        #3

        @jonb you are right! Thanks!

        1 Reply Last reply
        0
        • JonBJ JonB

          @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

          F Offline
          F Offline
          fem_dev
          wrote on last edited by
          #4

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

          VRoninV 1 Reply Last reply
          0
          • F fem_dev

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

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @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

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            3

            • Login

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