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

How should i store the results ??



  • Hello i have a simple Qprocess program which calls meson and returns results in JSON .
    [{"name": "myexe", "installed": false, "id": "myexe@exe", "filename": "myexe", "type": "executable", "build_by_default": true}]

    QCoreApplication a(argc, argv);
        QObject *parent = nullptr;
        QProcess *process = new QProcess(parent);
        const QString program  = "/usr/local/bin/meson";
        const QStringList args = {"introspect", "--targets", "/home/user/meson/builddir/"};
      
        process->start(program, args);
        int res = process->waitForFinished();
    
        if ( !res ) {
            qDebug() << process->errorString();
    
        }
        process->waitForFinished();
        QByteArray output = process->readAllStandardOutput();
        qDebug() << "output : " << output << "\n";
    

    It returns a QByteArray , how can i save it in json or something similar where i can extract each element

    i tried like this :

    JsonDocument item = QJsonDocument::fromJson(output);
        qDebug() << "item : " << item << "\n";
        QJsonArray rootobj = item.array();
        qDebug() << "rootobj : " << rootobj << "\n";
    
    

    but i am not able to access individual element .



  • Hi @Qjay,

    but i am not able to access individual element .

    Here's a basic example:

    const QByteArray output("[{\"name\": \"myexe\", \"installed\": false, \"id\": \"myexe@exe\", \"filename\": \"myexe\", \"type\": \"executable\", \"build_by_default\": true}]");
    qDebug().noquote() << output;
    
    const QJsonDocument doc = QJsonDocument::fromJson(output);
    qDebug().noquote() << doc;
    qDebug().noquote() << doc.array();
    qDebug().noquote() << doc.array().first();
    qDebug().noquote() << doc.array().first().toObject();
    qDebug().noquote() << doc.array().first().toObject().value("name");
    qDebug().noquote() << doc.array().first().toObject().value("name").toString();
    qDebug().noquote() << doc.array().first().toObject().value("installed");
    qDebug().noquote() << doc.array().first().toObject().value("installed").toBool();
    

    The output:

    [{"name": "myexe", "installed": false, "id": "myexe@exe", "filename": "myexe", "type": "executable", "build_by_default": true}]
    QJsonDocument([{"build_by_default":true,"filename":"myexe","id":"myexe@exe","installed":false,"name":"myexe","type":"executable"}])
    QJsonArray([{"build_by_default":true,"filename":"myexe","id":"myexe@exe","installed":false,"name":"myexe","type":"executable"}])
    QJsonValue(object, QJsonObject({"build_by_default":true,"filename":"myexe","id":"myexe@exe","installed":false,"name":"myexe","type":"executable"}))
    QJsonObject({"build_by_default":true,"filename":"myexe","id":"myexe@exe","installed":false,"name":"myexe","type":"executable"})
    QJsonValue(string, myexe)
    myexe
    QJsonValue(bool, false)
    false
    

    Above, I used .first() since the JSON array only has one item, but if you expected any number of items, you can loop through it like:

    const QJsonDocument doc = QJsonDocument::fromJson(output);
    qDebug().noquote() << doc;
    qDebug().noquote() << doc.array();
    foreach (auto value, doc.array()) {
        qDebug().noquote() << value;
        qDebug().noquote() << value.toObject();
        qDebug().noquote() << value.toObject().value("name");
        qDebug().noquote() << value.toObject().value("name").toString();
        qDebug().noquote() << value.toObject().value("installed");
        qDebug().noquote() << value.toObject().value("installed").toBool();
    }
    

    (same output)

    Cheers.



  • @Paul-Colby said in How should i store the results ??:

    qDebug().noquote() << doc;
    qDebug().noquote() << doc.array();
    foreach (auto value, doc.array()) {
    qDebug().noquote() << value;
    qDebug().noquote() << value.toObject();
    qDebug().noquote() << value.toObject().value("name");
    qDebug().noquote() << value.toObject().value("name").toString();
    qDebug().noquote() << value.toObject().value("installed");
    qDebug().noquote() << value.toObject().value("installed").toBool();
    }

    Thanks , exactly what i was looking for :)



  • @Paul-Colby how can i store the results as QVariantMap ?



  • Hi @Qjay,

    @Paul-Colby how can i store the results as QVariantMap ?

    Since the document is an array, you should convert it to a QVariantList rather than QVariantMap, otherwise, you can convert individual entries to QVariantMap objects. Here's a quick example of both:

    const QByteArray output("[{\"name\": \"myexe\", \"installed\": false, \"id\": \"myexe@exe\", \"filename\": \"myexe\", \"type\": \"executable\", \"build_by_default\": true}]");
    qDebug().noquote() << "bytes" << output;
    
    const QJsonDocument doc = QJsonDocument::fromJson(output);
    qDebug().noquote() << "doc:" << doc;
    qDebug().noquote() << "list:" << doc.array().toVariantList();
    foreach (auto value, doc.array()) {
        qDebug() << "map:" << value.toObject().toVariantMap();
    }
    

    Output:

    bytes [{"name": "myexe", "installed": false, "id": "myexe@exe", "filename": "myexe", "type": "executable", "build_by_default": true}]
    doc: QJsonDocument([{"build_by_default":true,"filename":"myexe","id":"myexe@exe","installed":false,"name":"myexe","type":"executable"}])
    list: (QVariant(QVariantMap, QMap((build_by_default, QVariant(bool, true))(filename, QVariant(QString, myexe))(id, QVariant(QString, myexe@exe))(installed, QVariant(bool, false))(name, QVariant(QString, myexe))(type, QVariant(QString, executable)))))
    map: QMap(("build_by_default", QVariant(bool, true))("filename", QVariant(QString, "myexe"))("id", QVariant(QString, "myexe@exe"))("installed", QVariant(bool, false))("name", QVariant(QString, "myexe"))("type", QVariant(QString, "executable")))
    

    Cheers.



  • @Paul-Colby Thanks

    i myself did something like this :)

     QVariantMap json_map;
    
    
        const QJsonDocument doc = QJsonDocument::fromJson(output);
        qDebug().noquote() << doc;
        qDebug().noquote() << doc.array();
        foreach (auto value, doc.array()) {
            qDebug().noquote() << value;
            qDebug().noquote() << value.toObject();
            qDebug().noquote() << value.toObject().value("name");
            json_map["name"] = value.toObject().value("name").toString();
    
            json_map["installed"] = value.toObject().value("installed").toBool();
    
            json_map["id"] = value.toObject().value("id").toString();
    
         
            json_map["filename"] = value.toObject().value("filename").toString();
    
    
    
        }