Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to write data into JSON?



  • 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();
    }
    


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


Log in to reply