0

I have the following JSON/Array from AWS and I'm struggling to read it with PHP:

{
  "Type" : "Notification",
  "MessageId" : "666483cb-e012-51f2-8d66-d308d55efd98",
  "TopicArn" : "arn:aws:sns:us-east-1:848283244672:S-Notification-Queue",
  "Message" : "{\"notificationType\":\"Delivery\",\"mail\"}"
}

Essentially I need to access the "Message" part of the array and be able to use the value pairs within like "notificationType = Delivery".

I've tried looping through the array with a PHP foreach look and I've tried decoding the array as follows:

$message_data = json_decode($message,true);

however I'm still struggling to access the data within. Note: I have the data in a variable $message.

Any advice on how to access the message data?

Also hoping to access parts within the Message section like:

{\"name\":\"Subject\",\"value\":\"abc"}

www.singles.dating

thankyou

2 Answers 2

2

The reason is that Message is serialized json.

So it needs decoding also

$message_data = json_decode($message, true);
if(
  isset($message_data['Message']) AND !is_array($message_data['Message'])
) {
  $message_data['Message'] = json_decode($message_data['Message'], true);
}
Sign up to request clarification or add additional context in comments.

Comments

1

The message section of that JSON is itself a string of JSON, so after decoding the whole JSON object you will need to decode the message property again. Assuming that JSON is in a variable called $aws_notification, you can do this:

$message_json = json_decode( $aws_notification, true )[ 'Message' ];
$message_data = json_decode( $message_json, true );

Then use can access $message_data[ 'notificationType' ] and its other properties.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.