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 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?

    JonBJ 1 Reply Last reply
    0
    • R R-P-H

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

      Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on 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

        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?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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
        2
        • JonBJ JonB

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

          Christian EhrlicherC Paul ColbyP 2 Replies Last reply
          0
          • R R-P-H

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

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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

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

              Paul ColbyP Offline
              Paul ColbyP Offline
              Paul Colby
              wrote on 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

              • Login

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