[SOLVED] Empty QJsonDocument from file

  • Hi,

    I'm trying to parse an appSettigns.json file that holds some application settings, but when I set the json document with QJsonDocument::fromJson(file.readAll()) the json document stays empty.

    Here's the code:

    #include "jsonmanager.h"

    #include <QFile>
    #include <QDebug>
    #include <QJsonDocument>
    #include <QJsonObject>

    JsonManager::JsonManager(QWidget *parent)
    QFile file(":/config/appSettings.json");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    qDebug() << "Could not open file";
    qDebug() << file.errorString();

    QJsonDocument   jsonDoc;
    jsonDoc = QJsonDocument::fromJson(file.readAll());
    if (!jsonDoc.isNull()) {
        qDebug() << "Could not read data";
    QJsonObject jsonObj = jsonDoc.object();
    QVariantMap jsonMap = jsonObj.toVariantMap();
    qDebug() << jsonMap.size();  // outputs 0
    // and so does jsonDoc.array().size();


    "server": {
    "path": "/path/to/executable",
    "ip": "",
    "port": 54321
    "data_path": "/path/to/data/folder",
    "frame_rate": 25,
    "vertical_sync": true,
    "threshold": 64,
    "camera": {
    "device_id": 0,
    "width": 640,
    "height": 480,
    "rotate_180": false,
    "use_uvc": false
    "background_color": {
    "red": 64,
    "green": 64,
    "blue": 64,
    "alpha": 255,
    "tool_highlight_color": {
    "red": 240,
    "green": 64,
    "blue": 64,
    "alpha": 128,

    I also checked the array in the QJsonDocument but it's empty, too. Any hints on what I'm doing wrong?


  • In the background_color object, there is a superfluous comma, as well as in tool_highlight_color. Maybe that is your problem.

  • Thanks for the quick answer.

    Unfortunately, even though you are right, removing the superfluous comma did not solve the problem. The QDomDocument is still empty.

  • I think the error is in the loading of the file.
    @qDebug() << file.bytesAvailable();@

    outputs 0, although it seems to load fine (no error output).

  • If I load the data from the file to QDomDocument object it seems to work fine. I very puzzled.

  • Lifetime Qt Champion


    Just to be on the safe side, did you properly put your json file in the resources (qrc file) ?

    You keep talking about QDomDocument, is it a typo ?

  • Yes, the file is properly put in my qrc file.

    And yes, sorry, when I said

    bq. Unfortunately, even though you are right, removing the superfluous comma did not solve the problem. The QDomDocument is still empty.

    I meant the QJsonDocument is still empty.

    But later on I tried to load the data into a QDomDocument object just to try and it seems to work fine.
    QDomDocument *domDocument;
    if (!domDocument->setContent(&file)) {
    qDebug() << "Could not set DOM";

    qDebug() << domDocument->toString();@

    prints the file contents to the console.

    Sorry for the confusion I created with the typo.

  • Lifetime Qt Champion

    Ok, then what does

    @qDebug() << file.readAll()@

    return ?

  • It returns

  • Lifetime Qt Champion

    Then it seems that your file is empty

  • That's what I thought, but
    @qDebug() << QString::fromUtf8(file.readAll())@
    returns the file contents.

  • Might it be an encoding issue?

  • Sorry, that was not completely right:
    @QString jsonString = QString::fromUtf8(file.readAll());
    qDebug() << jsonString;
    returns the file contents, while
    @qDebug() << QString::fromUtf8(file.readAll())@

  • Lifetime Qt Champion

    Strange, QJsonDocument should handle it...
    Did you check what error you got when using fromJson ?

  • No, I didn't. How could I do that?

  • Ok, I got it. A friend of mine pointed me out that I was looking for the array in the QJsonDocument, and that I should probably be looking for the object, which worked.

    The only thing that doesn't work right is that
    @QString jsonString = QString::fromUtf8(file.readAll());

    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());@
    works, but
    @QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll());@

  • Lifetime Qt Champion

    Look at the second parameter of "fromJson":http://qt-project.org/doc/qt-5.0/qtcore/qjsondocument.html#fromJson

    From there you can get the error

  • It's an 'Illegal value' error. But I get the same error even with an empty file.

  • Lifetime Qt Champion

    Could it be your json file encoding that is problematic ?

  • That might be it. Is there a way to make sure the file is UTF8? Or set it to UTF8?

  • Lifetime Qt Champion

    If you are using QtCreator you could check the editor settings to ensure that it uses UTF8

  • Ah, that did it!

    Thank you very much for all your help SGaist!

  • Lifetime Qt Champion

    You're welcome !

    If everything is working now, please update the thread's title to solved so other forum users may know a solution has been found :)

  • Hi,

    I have the same problem, but the solution isn't the UTF8 coding, for me.
    QtCreator is already configured to UTF, and that
    @ QString jsonString = QString::fromUtf8(file.readAll());

    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());

    don't work for me.

    My JSON file is built and written by JsonDocument and QFile, so, it could be valide:
    bool JSONStorage::storeJSONFile(){
    if( !jsonFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate) ){
    return false;
    QJsonDocument jsonDocument( jsonArray );
    jsonFile.write( jsonDocument.toJson() );
    return true;

    But I do not manage to load this file. I use this method:
    void JSONStorage::openJSONFile(){
    QJsonParseError parseErr;
    QJsonDocument jsonDocument;

    if( !jsonFile.open(QIODevice::ReadOnly | QIODevice::Text) ){
    jsonDocument.fromJson( jsonFile.readAll(), &parseErr );
    qDebug() << parseErr.errorString();
    if( jsonDocument.isEmpty()){
        qDebug() << "Empty!";
    if( jsonDocument.isArray() ){
        qDebug() << "Is array";
        jsonArray = jsonDocument.array();


    It says that is empty, and parseErr: "no error occurred"

    If I write:
    qDebug() << jsonFile.readAll();

    I can see my document. It isn't empty!

    What is wrong?

  • Is it because my main object is an array?

    I go to eating and I will retry with an JsonObject to the root, later.

  • Lifetime Qt Champion

    Use the second parameter of fromJson(const QByteArray & json, QJsonParseError * error = 0) and check what it returns

  • Ok

    It isn't that:
    jsonDocument.fromJson( jsonFile.readAll(), &parseErr );
    but that:
    jsonDocument = QJsonDocument::fromJson(jsonFile.readAll(), &parseErr );


  • Lifetime Qt Champion

    Indeed, fromJson is a static function

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.