kanaparthikiran
kanaparthikiran

Reputation: 523

Remove Double Encoding in Java Lambda Response

Getting Lambda Response as follows, which is double encoded.

"{\"id\":\"6EE1DDABDC5C5EB271289A057DDA82B1\",\"name\":\"Test\",\"stateName\":\"INITIAL\",\"description\":\"Test Description\",\"type\":\"Download\",\"createTime\":\"Aug 10, 2018 2:02:02 PM\",\"updateTime\":\"Aug 10, 2018 2:02:02 PM\"}"

However it shows fine when I print it under the handleRequest(Object input, Context context) method.

{"id":"6EE1DDABDC5C5EB271289A057DDA82B1","name":"Test","stateName":"INITIAL", "description":"Test Description","type":"Test","createTime":"Aug 10, 2018 2:02:02 PM", "updateTime":"Aug 10, 2018 2:02:02 PM"}

Any solutions to make it appear correctly in the response?

Upvotes: 0

Views: 809

Answers (2)

Tim Swetonic
Tim Swetonic

Reputation: 19

I recently solved this problem by changing my Lambda function to return a Java Object - I created a response class that had public get methods, which the Lambda runtime converted to JSON. No doubly-encoded JSON. Not sure why this isn't documented.

Upvotes: 1

erik258
erik258

Reputation: 16304

Your lambda returns json, which is really just a string that is valid json you can decode. But the lambda response is also json. So to send your json within the lambda response object, it is included as a string. It's not wrong in my mind to call this "double encoded", and just like the name implies, you'll have to decode twice as well: one for the outside json and once for the inside Json.

When you print from inside the lambda, your response hasn't been encoded into a containing json object, which is why it isn't double encoded there.

Upvotes: 4

Related Questions