Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to extract non {"key":"value"} JSON ?
QtWS25 Last Chance

How to extract non {"key":"value"} JSON ?

Scheduled Pinned Locked Moved Solved General and Desktop
jsonqjsondocumentqjsonvalueqbytearray
5 Posts 4 Posters 439 Views
  • 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.
  • R Offline
    R Offline
    R-P-H
    wrote on 16 Jun 2023, 13:42 last edited by R-P-H
    #1

    Hi, I am receiving a JSON response using the following code:

    QByteArray json = reply->readAll();
    QJsonDocument data = QJsonDocument::fromJson(json);
    

    This works perfectly for JSON such as:

    {"mykey": "myvalue"}
    

    which I can then access using data["mykey"].toString().

    However, when I get a JSON response such as json = ""My value"", data is NULL.

    How can I extract the JSON text correctly?

    J 1 Reply Last reply 16 Jun 2023, 14:08
    0
    • R R-P-H
      16 Jun 2023, 17:30

      @JonB Besides converting the QByteArray to a QString, how would I extract the value if I added {} to the QByteArray so that it becomes {""My value""} and converted it to a QJsonDocument ?

      P Offline
      P Offline
      Paul Colby
      wrote on 17 Jun 2023, 01:58 last edited by
      #5

      HI @R-P-H ,

      Besides converting the QByteArray to a QString, how would I extract the value if I added {} to the QByteArray ...

      As long as the QByteArray contains a valid JSON value, you can either wrap it in [ and ] to make it a JSON array, or { "key": and } to make it a JSON object. If wrapping in an array, you would then fetch the first array item to get the original JSON value. Of if wrapping in an object, fetch the value corresponding to the key you used in the wrapper.

      Here's a complete example:

          const QByteArray json{ "\"My Value\" "};
          qDebug().noquote() << "input               " << json;
          qDebug().noquote() << "parse-as-is         " << QJsonDocument::fromJson(json); // Will fail.
      
          const QJsonDocument arrayDoc = QJsonDocument::fromJson("[" + json + "]");
          qDebug().noquote() << "wrapped-in-array    " << arrayDoc;
          qDebug().noquote() << "unwrapped-array     " << arrayDoc.array().at(0);
      
          const QJsonDocument objectDoc = QJsonDocument::fromJson("{ \"value\": " + json + "}");
          qDebug().noquote() << "wrapped-in-object   " << objectDoc;
          qDebug().noquote() << "unwrapped-object    " << objectDoc.object().value(QStringLiteral("value"));
      

      Which outputs:

      input                "My Value" 
      parse-as-is          QJsonDocument()
      wrapped-in-array     QJsonDocument(["My Value"])
      unwrapped-array      QJsonValue(string, My Value)
      wrapped-in-object    QJsonDocument({"value":"My Value"})
      unwrapped-object     QJsonValue(string, My Value)
      

      Cheers.

      1 Reply Last reply
      3
      • R R-P-H
        16 Jun 2023, 13:42

        Hi, I am receiving a JSON response using the following code:

        QByteArray json = reply->readAll();
        QJsonDocument data = QJsonDocument::fromJson(json);
        

        This works perfectly for JSON such as:

        {"mykey": "myvalue"}
        

        which I can then access using data["mykey"].toString().

        However, when I get a JSON response such as json = ""My value"", data is NULL.

        How can I extract the JSON text correctly?

        J Offline
        J Offline
        JonB
        wrote on 16 Jun 2023, 14:08 last edited by
        #2

        @R-P-H
        A complete response of ""My Value"", or for that matter "My Value", is not a JSON document. A JSON document must start with either [ (array) or { (object).

        R 1 Reply Last reply 16 Jun 2023, 17:30
        2
        • J JonB
          16 Jun 2023, 14:08

          @R-P-H
          A complete response of ""My Value"", or for that matter "My Value", is not a JSON document. A JSON document must start with either [ (array) or { (object).

          R Offline
          R Offline
          R-P-H
          wrote on 16 Jun 2023, 17:30 last edited by
          #3

          @JonB Besides converting the QByteArray to a QString, how would I extract the value if I added {} to the QByteArray so that it becomes {""My value""} and converted it to a QJsonDocument ?

          C P 2 Replies Last reply 16 Jun 2023, 17:36
          0
          • R R-P-H
            16 Jun 2023, 17:30

            @JonB Besides converting the QByteArray to a QString, how would I extract the value if I added {} to the QByteArray so that it becomes {""My value""} and converted it to a QJsonDocument ?

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 16 Jun 2023, 17:36 last edited by
            #4

            Since {"myvalue²} is no valid json - never.
            See https://www.json.org/json-en.html

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            2
            • R R-P-H
              16 Jun 2023, 17:30

              @JonB Besides converting the QByteArray to a QString, how would I extract the value if I added {} to the QByteArray so that it becomes {""My value""} and converted it to a QJsonDocument ?

              P Offline
              P Offline
              Paul Colby
              wrote on 17 Jun 2023, 01:58 last edited by
              #5

              HI @R-P-H ,

              Besides converting the QByteArray to a QString, how would I extract the value if I added {} to the QByteArray ...

              As long as the QByteArray contains a valid JSON value, you can either wrap it in [ and ] to make it a JSON array, or { "key": and } to make it a JSON object. If wrapping in an array, you would then fetch the first array item to get the original JSON value. Of if wrapping in an object, fetch the value corresponding to the key you used in the wrapper.

              Here's a complete example:

                  const QByteArray json{ "\"My Value\" "};
                  qDebug().noquote() << "input               " << json;
                  qDebug().noquote() << "parse-as-is         " << QJsonDocument::fromJson(json); // Will fail.
              
                  const QJsonDocument arrayDoc = QJsonDocument::fromJson("[" + json + "]");
                  qDebug().noquote() << "wrapped-in-array    " << arrayDoc;
                  qDebug().noquote() << "unwrapped-array     " << arrayDoc.array().at(0);
              
                  const QJsonDocument objectDoc = QJsonDocument::fromJson("{ \"value\": " + json + "}");
                  qDebug().noquote() << "wrapped-in-object   " << objectDoc;
                  qDebug().noquote() << "unwrapped-object    " << objectDoc.object().value(QStringLiteral("value"));
              

              Which outputs:

              input                "My Value" 
              parse-as-is          QJsonDocument()
              wrapped-in-array     QJsonDocument(["My Value"])
              unwrapped-array      QJsonValue(string, My Value)
              wrapped-in-object    QJsonDocument({"value":"My Value"})
              unwrapped-object     QJsonValue(string, My Value)
              

              Cheers.

              1 Reply Last reply
              3
              • R R-P-H has marked this topic as solved on 20 Jun 2023, 21:05

              1/5

              16 Jun 2023, 13:42

              • Login

              • Login or register to search.
              1 out of 5
              • First post
                1/5
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved