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

|HELP| : How to extract Data from Json file ?



  • I have a Json file wich has the format starting with a "[" , and has objects inside it, each object has its own attributes. My purpose is to retrieve these data of each object and insert it in a database, that's another talk but for the moment I want just to print them. I was struggling with this for about 4 days without finding what I need.
    here is an example of my file :

    [
     {
        "updated":1592183850402,
        "country":"Morocco",
        "countryInfo":
        {"_id":504,
            "iso2":"MA",
            "iso3":"MAR",
            "lat":32,
            "continent":"Africa",
            "activePerOneMillion":22.12
        }
    },
    {
        "updated":159388477402,
        "country":"France",
        "countryInfo":
        {"_id":504,
            "iso2":"FR",
            "iso3":"FRA",
            "lat":332,
            "continent":"Europe",
            "activePerOneMillion":66.12
        }
    }
    ]
    

    And I really need help, since I'm new to both Qt and Json I can't go so far by myself. if any one can help me with a piece of code.
    this is what I wrote, but whenever I reache the foreach loop I can't figure out my next move. I have tried different ways inside the loop but none of it worked.

        QTextStream file_text(&my_json);
        QString json_string;
        json_string = file_text.readAll();
        my_json.close();
        QByteArray json_bytes = json_string.toLocal8Bit();
        auto json_doc = QJsonDocument::fromJson(json_bytes);
        QJsonObject json_obj = json_doc.object();
        QJsonArray jsonArray = json_obj["activePerOneMillion"].toArray();
    
        foreach (const QJsonValue & value, jsonArray) { ... }
    


  • Thank you all. And a special thanks to @LeLev who helped me in private. apparently the base-ranged for loop helped me since it reconizes the type of objects that are stored in the QjsonArray so it access them all. This was what worked for me:

     QFile countries_file("countries.json");
        QFile morocco_file("morocco.json");
        QString json_string;
    
        if(countries_file.open(QIODevice::ReadOnly | QIODevice::Text)){
            json_string = countries_file.readAll();
            countries_file.close();
        }
        else
            qDebug()<< "file not found";
        auto json_doc = QJsonDocument::fromJson(json_string.toUtf8());
    
        QJsonArray jArr = json_doc.array();
    QJsonValue val;
        for(auto jsonObj : jArr)
        {
            val = jsonObj.toObject().value("country");
            _country = val.toString();
            qDebug() << "Country: "<< _country;
    
            val = jsonObj.toObject().value("cases");
            _cases = val.toInt();
            qDebug() << "Cases: "<< _cases;
    
            val = jsonObj.toObject().value("deaths");
            _deaths = val.toInt();
            qDebug() << "Deaths: "<< _deaths;
       }
    
    
            qDebug() << Qt::endl;
            qDebug() << Qt::endl;
    
    

  • Lifetime Qt Champion

    Hi and welcome to the forums.
    You seem off to a good start.
    What you have is a json array with objects in it.

    So in you for loop you will get QJsonObject and not a
    QJsonValue if i read it correctly :=)

    Nope. @JonB is right :)



  • @Elmehdi said in |HELP| : How to extract Data from Json file ?:

    I'm new to both Qt and Json

    Just in case, have you read the documentation?

    There's even a good example...



  • @Elmehdi
    With all die respect to my learned friend @mrjj, you do get QJsonValues as you iterate through QJsonArray jsonArray.

    The point is, a QJsonValue can hold many data types (see https://doc.qt.io/qt-5/qjsonvalue.html#details). In your case those will be QJsonObjects. The first thing you should write in your code is:

    foreach (const QJsonValue & value, jsonArray)
    {
        qDebug() << value.type();
    }
    

    and look at the values per https://doc.qt.io/qt-5/qjsonvalue.html#Type-enum.

    From there, you will probably also want to look at QJsonObject QJsonValue::toObject() and/or QVariant QJsonValue::toVariant().

    If you know about recursion, that will be pretty handy when parsing JSON data.


  • Lifetime Qt Champion

    Hi
    I think what confused me is this
    QJsonArray jsonArray = json_obj["cases"].toArray();
    i dont see "cases" in the data sample.



  • @mrjj
    I did indeed intend to mention to @Elmehdi : if that is the complete JSON file, nothing there matches with the json_obj["cases"] in your code, I don't know where your idea of the "cases" comes from.



  • it is not valide json format, you have to delete the semicolon at the end of last lines "activePerOneMillion":66.12,


  • Lifetime Qt Champion

    @LeLev
    Thats good spotting!



  • Thank you guys, I didn't expect the fast answers, that was great. @mrjj sorry there is no field with that name I corrected it.
    @JonB I will check that immediately thanks.



  • @LeLev you are right I just made a mistake while copying since it was not the whole file it was just a part of it, I fixed it now.



  • Thank you all. And a special thanks to @LeLev who helped me in private. apparently the base-ranged for loop helped me since it reconizes the type of objects that are stored in the QjsonArray so it access them all. This was what worked for me:

     QFile countries_file("countries.json");
        QFile morocco_file("morocco.json");
        QString json_string;
    
        if(countries_file.open(QIODevice::ReadOnly | QIODevice::Text)){
            json_string = countries_file.readAll();
            countries_file.close();
        }
        else
            qDebug()<< "file not found";
        auto json_doc = QJsonDocument::fromJson(json_string.toUtf8());
    
        QJsonArray jArr = json_doc.array();
    QJsonValue val;
        for(auto jsonObj : jArr)
        {
            val = jsonObj.toObject().value("country");
            _country = val.toString();
            qDebug() << "Country: "<< _country;
    
            val = jsonObj.toObject().value("cases");
            _cases = val.toInt();
            qDebug() << "Cases: "<< _cases;
    
            val = jsonObj.toObject().value("deaths");
            _deaths = val.toInt();
            qDebug() << "Deaths: "<< _deaths;
       }
    
    
            qDebug() << Qt::endl;
            qDebug() << Qt::endl;
    
    

Log in to reply