Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to write data into JSON?
Forum Updated to NodeBB v4.3 + New Features

How to write data into JSON?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 1.7k Views 1 Watching
  • 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.
  • L Offline
    L Offline
    lakshmanGiri
    wrote on last edited by
    #1

    I have a simple window where I will get data like Name, email id, and password from the user. I wanted to store these data in a JSON file. With the help of QML context, I have integrated QML and qt and also I was able to write the data into a JSON file. But, I am facing a problem here.

    I want the JSON data to look something like this:```

    {
        "loginDetails": {
            "userLoginDetails": [
                {
                    "Email-id": "test",
                    "Name": "test",
                    "Password": "ets"
                },
                {
                    "Email-id": "lakshmn",
                    "Name": "laskhman",
                    "Password": "giri123"
                }
            ]
        }
    }
    

    But, the JSON fie what I get is different:

    {
        "loginDetails": {
            "userLoginDetails": [
                {
                    "EMAIL_ID": "test1",
                    "NAME": "test1",
                    "PASSWORD": "tset12"
                }
            ]
        }
    }
    
    {
        "loginDetails": {
            "userLoginDetails": [
                {
                    "EMAIL_ID": "test1",
                    "NAME": "test1",
                    "PASSWORD": "tset12"
                }
            ]
        }
    }
    

    Thus, every time I execute, It is creating a new JSON object.I wanted to append the values to the existing JSON object, how should I do this? can anyone please help? Thanks in advance.

    sample code:

    void writeJson::getDataFromQml(QString a, QString b, QString c)
    {
        // Checking the data
        qDebug() << "Value 1: " << a;
        qDebug() << "Value 2: " << b;
        qDebug() << "Value 3: " << c;
    
        // Checking for the file location
           QFile file(path);
           file.open(QIODevice::ReadWrite | QIODevice::Text);
           QJsonParseError JsonParseError;
           QJsonDocument JsonDocument = QJsonDocument::fromJson(file.readAll(), &JsonParseError);
    
           // creating a json object and getting data
           QJsonObject loginDetails;
           loginDetails["NAME"] = a;
           loginDetails["EMAIL_ID"] = b;
           loginDetails["PASSWORD"] = c;
    
           // Appending the data to JSON array
           QJsonArray userLoginDetails;
    
           userLoginDetails.push_back(loginDetails);
    
          // Assigning the array to a json object
           QJsonObject obj;
           obj [ "userLoginDetails" ] = userLoginDetails;
    
           // writing the json data to the file
           QJsonObject jsonObject;
           jsonObject["loginDetails"] = obj;
           JsonDocument.setObject(jsonObject);
           file.open(QFile::WriteOnly | QFile::Text | QFile::Append);
           file.write(JsonDocument.toJson(QJsonDocument::Indented));
           file.close();
    }
    
    JonBJ 1 Reply Last reply
    0
    • L lakshmanGiri

      I have a simple window where I will get data like Name, email id, and password from the user. I wanted to store these data in a JSON file. With the help of QML context, I have integrated QML and qt and also I was able to write the data into a JSON file. But, I am facing a problem here.

      I want the JSON data to look something like this:```

      {
          "loginDetails": {
              "userLoginDetails": [
                  {
                      "Email-id": "test",
                      "Name": "test",
                      "Password": "ets"
                  },
                  {
                      "Email-id": "lakshmn",
                      "Name": "laskhman",
                      "Password": "giri123"
                  }
              ]
          }
      }
      

      But, the JSON fie what I get is different:

      {
          "loginDetails": {
              "userLoginDetails": [
                  {
                      "EMAIL_ID": "test1",
                      "NAME": "test1",
                      "PASSWORD": "tset12"
                  }
              ]
          }
      }
      
      {
          "loginDetails": {
              "userLoginDetails": [
                  {
                      "EMAIL_ID": "test1",
                      "NAME": "test1",
                      "PASSWORD": "tset12"
                  }
              ]
          }
      }
      

      Thus, every time I execute, It is creating a new JSON object.I wanted to append the values to the existing JSON object, how should I do this? can anyone please help? Thanks in advance.

      sample code:

      void writeJson::getDataFromQml(QString a, QString b, QString c)
      {
          // Checking the data
          qDebug() << "Value 1: " << a;
          qDebug() << "Value 2: " << b;
          qDebug() << "Value 3: " << c;
      
          // Checking for the file location
             QFile file(path);
             file.open(QIODevice::ReadWrite | QIODevice::Text);
             QJsonParseError JsonParseError;
             QJsonDocument JsonDocument = QJsonDocument::fromJson(file.readAll(), &JsonParseError);
      
             // creating a json object and getting data
             QJsonObject loginDetails;
             loginDetails["NAME"] = a;
             loginDetails["EMAIL_ID"] = b;
             loginDetails["PASSWORD"] = c;
      
             // Appending the data to JSON array
             QJsonArray userLoginDetails;
      
             userLoginDetails.push_back(loginDetails);
      
            // Assigning the array to a json object
             QJsonObject obj;
             obj [ "userLoginDetails" ] = userLoginDetails;
      
             // writing the json data to the file
             QJsonObject jsonObject;
             jsonObject["loginDetails"] = obj;
             JsonDocument.setObject(jsonObject);
             file.open(QFile::WriteOnly | QFile::Text | QFile::Append);
             file.write(JsonDocument.toJson(QJsonDocument::Indented));
             file.close();
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @lakshmanGiri
      You are opening the file for append when writing back. This cannot be right, if you look at what you want it is not simply an append to what you had. You must read in the existing JSON, create the objects in memory, do your work (find the node you want to append to and append an object in memory), then finally write the whole of the new JSON out to the file, completely overwriting what was there.

      1 Reply Last reply
      1

      • Login

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