I am writing a Lambda function that receives an SQS queue object. The SQS sends a json object as a string value to SQS.
When I receive the request in the Lambda, AWS has wrapped it into a new JSON and since the json is a string value it becomes invalid json.
(Example) The json looks like:
{"uuid ":"someuuid"}
We send this as a string to the SQS. AWS then wraps this into something like:
{
"Records": [
{
"messageId": "somemesasgeid",
"receiptHandle": "MessageReceiptHandle",
"body": {
"Message":"{"uuid":"someUuid"}"
},
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "sometimestamp",
"SenderId": "someid",
"ApproximateFirstReceiveTimestamp": "sometimestamp"
},
"messageAttributes": {},
"md5OfBody": "somebody",
"eventSource": "aws:sqs",
"eventSourceARN": "someARN",
"awsRegion": "eu-west-1"
}
]
}
Now the body.Message is not valid Json. I tried parsing it as a raw value like How can I include raw JSON in an object using Jackson? but it keeps complaining that it found a u where it was expecting a comma seperate object.
Since I can't post raw json to SQS and have to stringify it, how do I go about parsing this into an object where I can get the json message?
I tried creating a pojo and trying the above link, but jackson keeps complaining about the message variable.
--- update with code ---
private Response HandleServiceRequest(Map<String, Object> input) {
List<String> records = (List<String>) input.get("Records");
for(String r : records) {
SqsMessage m = objectMapper.readValue(r, SqsMessage.class);
}
}
public class SqsMessage {
// all other values
SqsBody body;
// getters/setters
}
public class SqsBody {
// all other values
@JsonProperty("Message")
private Object message;
// getters/setters
@JsonRawValue
public String getMessage() {
message == null ? null : message.toString();
}
public void setMessage(Object message){
this.message = message;
}
}
This is what I have now. I tried changing message to String but that did not change anything.