AWS SQS JSON format when receiving message from SNS with Ruby SDK
amazon-sns, amazon-sqs, amazon-web-services, json, ruby
Solution
Found the answer.
The problem was the way I was getting the JSON. I needed to use `JSON.load(result['Message'])`, instead of `JSON.parse(...)`.
Problem
I have an SQS queue which is subscribed to a SNS topic. When I publish a new notification to the topic, I use the following code (within a Sinatra app): ``` jsonMessage = { "announcement" => { "first_name" => results['first_name'][:s], "last_name" => results['last_name'][:s], "loc_code" => results['location'][:s], "note" => params['note_content'] } } msgid = @announcments_topic.publish(jsonMessage.to_json, {subject: "Note Created", message_structure: 'json' }) ``` When my queue listener picks up this notification, the message section of the corresponding hash looks like this: ``` "Message"=>"{\"announcement\":{\"first_name\":\"Eve\",\"last_name\":\"Salt\",\"loc_code\":\"Location\",\"note\":\"test\"}}" ``` In my queue listener, I want to use this hash, but when I try to use ``` JSON.parse(result['Message']) ``` I get an unexpected token error because of the escaped double quotes. Any suggestions on how I can fix this? Am I not sending my notification as JSON properly? How can I get sns/sqs to not escape the double quotes?