Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Format JSON
Qt 6.12 LTS is out! See what's new in the release blog

Format JSON

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 7 Posters 5.2k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T t0msk

    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:

    QJsonValue(undefined)
    

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

    kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by kshegunov
    #2

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

    Read and abide by the Qt Code of Conduct

    1 Reply Last reply
    3
    • M Offline
      M Offline
      Mr Gisa
      wrote on last edited by Mr Gisa
      #3

      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)

      T 1 Reply Last reply
      0
      • T t0msk

        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:

        QJsonValue(undefined)
        

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

        Taz742T Offline
        Taz742T Offline
        Taz742
        wrote on last edited by
        #4

        @t0msk said in Format JSON:

        QJsonDocument document = QJsonDocument::fromJson(reply);

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

        Do what you want.

        1 Reply Last reply
        1
        • M Mr Gisa

          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)

          T Offline
          T Offline
          t0msk
          wrote on last edited by
          #5

          @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:

          QJsonValue(undefined)
          

          @Taz742 Tried and didn't help.

          Student who loves C/C++

          mrjjM 1 Reply Last reply
          0
          • M Offline
            M Offline
            Mr Gisa
            wrote on last edited by Mr Gisa
            #6

            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 ".

            1 Reply Last reply
            0
            • T t0msk

              @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:

              QJsonValue(undefined)
              

              @Taz742 Tried and didn't help.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #7

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

              T 1 Reply Last reply
              2
              • mrjjM mrjj

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

                T Offline
                T Offline
                t0msk
                wrote on last edited by
                #8

                @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:

                QJsonValue(undefined)
                

                Student who loves C/C++

                mrjjM 1 Reply Last reply
                0
                • T t0msk

                  @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:

                  QJsonValue(undefined)
                  
                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #9

                  Hi
                  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
                  https://jsonlint.com/
                  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;
                  }

                  1 Reply Last reply
                  4
                  • T t0msk

                    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:

                    QJsonValue(undefined)
                    

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

                    Pablo J. RoginaP Offline
                    Pablo J. RoginaP Offline
                    Pablo J. Rogina
                    wrote on last edited by
                    #10

                    @t0msk said in Format JSON:

                    Hello, I get this JSON from one webservice:

                    What a network traffic capture shows about data actually received?

                    Upvote the answer(s) that helped you solve the issue
                    Use "Topic Tools" button to mark your post as Solved
                    Add screenshots via postimage.org
                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #11

                      Can you try printing the output of

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

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt
                      • Unsolved