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. parse json response body which is also json object
Forum Updated to NodeBB v4.3 + New Features

parse json response body which is also json object

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 997 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.
  • T Offline
    T Offline
    taedooly
    wrote on 26 Oct 2020, 10:20 last edited by
    #1

    hi, i'm trying to make QT application with my aws DB and api gateway.

    in my python code, i created response return like this. Json body in Json response

    return {
                'isBase64Encoded': False,
                'statusCode': 200,
                'body': json.dumps(json_data, ensure_ascii = False)
                }    
    

    in my qt code there is no problem calling 'statusCode' and 'isBase64Encoded'. But when i access to 'body', Qt doesn't read my 'body' value as Json format.

    With qDebug() << reply["body"], it prints this

    QJsonValue(string, "[{\"name\": \"아무개\", \"value\": 200, \"date\": \"2020-10-05\"}, {\"name\": \"asd\", \"value\": 200, \"date\": \"2020-10-05\"}, {\"name\": \"test\", \"value\": 150, \"date\": \"2020-10-07\"}, {\"name\": \"qre\", \"value\": 300, \"date\": \"2020-10-07\"}, {\"name\": \"qre\", \"value\": 300, \"date\": \"2020-10-23\"}, {\"name\": \"test\", \"value\": 150, \"date\": \"2020-10-23\"}]")
    

    Because aws lambda proxy requires certain response format, it's not possible to change response format.
    I'm trying to recreate Json with that string value. But i don't know what Qt method i should use.

    when i use

    qDebug() << reply.value("body").toObject(), 
    

    it returns empty object.

    Did i something wrong with JSON format response? or is there any function can solve my problem? please notice me with little example.

    If you have some knowledge in JSON and AWS please help me.

    J V 3 Replies Last reply 26 Oct 2020, 10:28
    0
    • T taedooly
      26 Oct 2020, 10:20

      hi, i'm trying to make QT application with my aws DB and api gateway.

      in my python code, i created response return like this. Json body in Json response

      return {
                  'isBase64Encoded': False,
                  'statusCode': 200,
                  'body': json.dumps(json_data, ensure_ascii = False)
                  }    
      

      in my qt code there is no problem calling 'statusCode' and 'isBase64Encoded'. But when i access to 'body', Qt doesn't read my 'body' value as Json format.

      With qDebug() << reply["body"], it prints this

      QJsonValue(string, "[{\"name\": \"아무개\", \"value\": 200, \"date\": \"2020-10-05\"}, {\"name\": \"asd\", \"value\": 200, \"date\": \"2020-10-05\"}, {\"name\": \"test\", \"value\": 150, \"date\": \"2020-10-07\"}, {\"name\": \"qre\", \"value\": 300, \"date\": \"2020-10-07\"}, {\"name\": \"qre\", \"value\": 300, \"date\": \"2020-10-23\"}, {\"name\": \"test\", \"value\": 150, \"date\": \"2020-10-23\"}]")
      

      Because aws lambda proxy requires certain response format, it's not possible to change response format.
      I'm trying to recreate Json with that string value. But i don't know what Qt method i should use.

      when i use

      qDebug() << reply.value("body").toObject(), 
      

      it returns empty object.

      Did i something wrong with JSON format response? or is there any function can solve my problem? please notice me with little example.

      If you have some knowledge in JSON and AWS please help me.

      J Offline
      J Offline
      JonB
      wrote on 26 Oct 2020, 10:28 last edited by JonB
      #2

      @taedooly
      Seems to me your qDebug() << reply["body"] output is correct. It's a string value. Since you produced it via json.dumps() you need to parse it back to a Python object (looks like it was an array of objects) via json.loads().

      T 1 Reply Last reply 26 Oct 2020, 10:33
      1
      • J JonB
        26 Oct 2020, 10:28

        @taedooly
        Seems to me your qDebug() << reply["body"] output is correct. It's a string value. Since you produced it via json.dumps() you need to parse it back to a Python object (looks like it was an array of objects) via json.loads().

        T Offline
        T Offline
        taedooly
        wrote on 26 Oct 2020, 10:33 last edited by
        #3

        @JonB i uses c++ in Qt. but i know that json.loads() is Python function, is there matching function in C++??

        J 1 Reply Last reply 26 Oct 2020, 10:47
        0
        • T taedooly
          26 Oct 2020, 10:20

          hi, i'm trying to make QT application with my aws DB and api gateway.

          in my python code, i created response return like this. Json body in Json response

          return {
                      'isBase64Encoded': False,
                      'statusCode': 200,
                      'body': json.dumps(json_data, ensure_ascii = False)
                      }    
          

          in my qt code there is no problem calling 'statusCode' and 'isBase64Encoded'. But when i access to 'body', Qt doesn't read my 'body' value as Json format.

          With qDebug() << reply["body"], it prints this

          QJsonValue(string, "[{\"name\": \"아무개\", \"value\": 200, \"date\": \"2020-10-05\"}, {\"name\": \"asd\", \"value\": 200, \"date\": \"2020-10-05\"}, {\"name\": \"test\", \"value\": 150, \"date\": \"2020-10-07\"}, {\"name\": \"qre\", \"value\": 300, \"date\": \"2020-10-07\"}, {\"name\": \"qre\", \"value\": 300, \"date\": \"2020-10-23\"}, {\"name\": \"test\", \"value\": 150, \"date\": \"2020-10-23\"}]")
          

          Because aws lambda proxy requires certain response format, it's not possible to change response format.
          I'm trying to recreate Json with that string value. But i don't know what Qt method i should use.

          when i use

          qDebug() << reply.value("body").toObject(), 
          

          it returns empty object.

          Did i something wrong with JSON format response? or is there any function can solve my problem? please notice me with little example.

          If you have some knowledge in JSON and AWS please help me.

          V Offline
          V Offline
          VRonin
          wrote on 26 Oct 2020, 10:41 last edited by
          #4

          @taedooly said in parse json response body which is also json object:

          when i use
          qDebug() << reply.value("body").toObject(),

          it returns empty object.

          Because it's not an object, you can clearly see from the printout you posted it's an array so you have to use reply.value("body").toArray(). You can use QJsonValue::type to get the program to tell you what is contained in your value

          "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

          T 1 Reply Last reply 26 Oct 2020, 10:45
          3
          • V VRonin
            26 Oct 2020, 10:41

            @taedooly said in parse json response body which is also json object:

            when i use
            qDebug() << reply.value("body").toObject(),

            it returns empty object.

            Because it's not an object, you can clearly see from the printout you posted it's an array so you have to use reply.value("body").toArray(). You can use QJsonValue::type to get the program to tell you what is contained in your value

            T Offline
            T Offline
            taedooly
            wrote on 26 Oct 2020, 10:45 last edited by
            #5

            @VRonin
            I first tried toArray() function. but it create empty array( isArray() and isEmpty also returns false)
            Above as JonB said, value in 'body' is clearly string.

            1 Reply Last reply
            0
            • T taedooly
              26 Oct 2020, 10:33

              @JonB i uses c++ in Qt. but i know that json.loads() is Python function, is there matching function in C++??

              J Offline
              J Offline
              JonB
              wrote on 26 Oct 2020, 10:47 last edited by
              #6

              @taedooly said in parse json response body which is also json object:

              @JonB i uses c++ in Qt. but i know that json.loads() is Python function, is there matching function in C++??

              You show json.dumps(), which is Python. Am I to understand that the sender of the JSON is written in Python but the receiver is written is written in C++? If so this was not at all clear.

              If so, then you probably want to use QJsonDocument::fromJson(). Or some other suitable QJson... method as per the docs.

              1 Reply Last reply
              3
              • V Offline
                V Offline
                VRonin
                wrote on 26 Oct 2020, 10:47 last edited by VRonin
                #7

                QJsonArray bodyArray = QJsonDocument::fromJson(reply.value("body").toString()).array();

                "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
                3
                • T taedooly
                  26 Oct 2020, 10:20

                  hi, i'm trying to make QT application with my aws DB and api gateway.

                  in my python code, i created response return like this. Json body in Json response

                  return {
                              'isBase64Encoded': False,
                              'statusCode': 200,
                              'body': json.dumps(json_data, ensure_ascii = False)
                              }    
                  

                  in my qt code there is no problem calling 'statusCode' and 'isBase64Encoded'. But when i access to 'body', Qt doesn't read my 'body' value as Json format.

                  With qDebug() << reply["body"], it prints this

                  QJsonValue(string, "[{\"name\": \"아무개\", \"value\": 200, \"date\": \"2020-10-05\"}, {\"name\": \"asd\", \"value\": 200, \"date\": \"2020-10-05\"}, {\"name\": \"test\", \"value\": 150, \"date\": \"2020-10-07\"}, {\"name\": \"qre\", \"value\": 300, \"date\": \"2020-10-07\"}, {\"name\": \"qre\", \"value\": 300, \"date\": \"2020-10-23\"}, {\"name\": \"test\", \"value\": 150, \"date\": \"2020-10-23\"}]")
                  

                  Because aws lambda proxy requires certain response format, it's not possible to change response format.
                  I'm trying to recreate Json with that string value. But i don't know what Qt method i should use.

                  when i use

                  qDebug() << reply.value("body").toObject(), 
                  

                  it returns empty object.

                  Did i something wrong with JSON format response? or is there any function can solve my problem? please notice me with little example.

                  If you have some knowledge in JSON and AWS please help me.

                  J Offline
                  J Offline
                  JonB
                  wrote on 26 Oct 2020, 10:56 last edited by JonB
                  #8

                  @taedooly said in parse json response body which is also json object:

                  'body': json.dumps(json_data, ensure_ascii = False)

                  Very briefly, do you need to do the json.dumps() here at all? If you just sent back 'body': json_data it would have gotten JSON-encoded via json.dumps() anyway, you seem to be double encoding/decoding... Up to you. OIC, your response is not itself JSON-encoded before it is sent?

                  1 Reply Last reply
                  3
                  • T Offline
                    T Offline
                    taedooly
                    wrote on 26 Oct 2020, 10:56 last edited by taedooly
                    #9

                    QI declare new QString with reply.value('body') and convert it to Qjsondocument again and it works.

                    But @VRonin your solution is much more simple and reasonable. thank you :)

                    QJsonArray bodyArray = QJsonDocument::fromJson(reply.value("body").toString().toUtf8()).array();
                    

                    @JonB I don't know why but AWS lambda and API Gateway connection requires body part in only json

                    1 Reply Last reply
                    1

                    1/9

                    26 Oct 2020, 10:20

                    • Login

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