Qt World Summit: Register Today!

Format JSON

  • Hello, I get this JSON from one webservice:

    \"response_code\": 1,
    \"sha256\": \"c8e61eacbbh31ae51e50a0083e5429bb6bea0f4d822c2793cd58cbf17e3638c1\", 
    \"resource\": \"c8e61egcbbd31ae51e50a0093e5429bb6bea0fed822c2794cd58rbf17e3638c1\", 
    \"scan_id\": \"c8e61earbd31ae51e50a0093e5429bb6bee0f4d822c2794cd58cbf1fe3638c1e1531522520\"

    But if I do:

    QJsonDocument document = QJsonDocument::fromJson(reply);
    QJsonObject rootObj = document.object();
    qDebug() << rootObj.value(QString("scan_id"));

    I will get:


    So I think it is because that JSON is bad formated (it contains backslashes), is possible to format it into correct form?

  • Moderators

    Did you perchance get that output from qDebug()? If so use qDebug().noquote().

  • If the backslash is the only problem, why don't clear the json first using replace('\', '') - QByteArray & QByteArray::replace(const char * before, const char * after)

  • @t0msk said in Format JSON:

    QJsonDocument document = QJsonDocument::fromJson(reply);

    Did you try QJsonDocument document = QJsonDocument::fromBinaryData(reply); ?

  • @Mr-Gisa Replacing backslashes of whole reply, can be dangerous because it can corrupt data inside (if message contain backshlash).

    @kshegunov Problem is that I cannot get value of node:

    qDebug() << rootObj.value(QString("scan_id"));

    It says:


    @Taz742 Tried and didn't help.

  • You are right, but bare in mind that you won't find a method to remove the backslashes for you, you will have to sanitize the input yourself, perhaps you could instead of replacing the \ only, you could replace \" with ".

  • Lifetime Qt Champion

    You should try with
    as @Taz742 suggests.
    and the \ might NOT be real.
    Try outputting with qDebug().noquote() << reply

  • @mrjj Yea it removed backslahes, but why this doesn't work?

    QJsonDocument document = QJsonDocument::fromBinaryData(reply);
    QJsonObject rootObj = document.object();
    qDebug().noquote() << rootObj.value(QString("scan_id"));

    I still get:


  • Lifetime Qt Champion

    The returned QJsonValue is QJsonValue::Undefined if the key does not exist.
    so it sounds like it dont have "scan_id" key
    Try to dump what rootObj contains and see. what is in there.
    also try
    to see if likes the json.
    also reading the docs it seems
    QJsonDocument::fromBinaryData expects data to be a blob and i think you have
    just a string value so try with
    QJsonDocument::fromJson instead. ( or again. sorry)

    also check QJsonDocument if it parses correctly.
    You have no error checking so it could be anything!
    we are flying blind here :)
    something like. (there will be dragons. not compiled)
    QJsonParseError error;
    QJsonDocument document = QJsonDocument::fromJson(reply, &error );
    if (document.isNull() {
    qDebug() << "Error: " << error.errorString() << error.offset;

  • @t0msk said in Format JSON:

    Hello, I get this JSON from one webservice:

    What a network traffic capture shows about data actually received?

  • Can you try printing the output of

    qDebug() << document.isObject();
    qDebug() << rootObj.toVariantMap();

Log in to reply