parse json response body which is also json object
-
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.
-
-
@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 -
@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. -
@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 viajson.dumps()
anyway, you seem to be double encoding/decoding... Up to you. OIC, your response is not itself JSON-encoded before it is sent? -
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