I'm very new to python. Is there anyway to check specific JSON object exist in python except my following code? Admit that my following code will not be good practice so need to know which way to better to check and easy to maintain?
Here is JSON response:
[
{
"MessageId": "250e37a8-d779-48a1-9941-84219a82513e",
"ReceiptHandle": "AQEBjualXe2ywqTgIVmCNI5sKj7r48werf84HHA2BWZimwiEXLFxA/MiPBclK048NZBtOnM3dSDfoiwwqoNTPxTRz+IChd8McziweCxHX6texjAOi/MyAQjCWP+2hJPoxzQgXx9kjnKbepKlcgxhpOiQZe6WiSIq0dXwHHXSA7SP0g9NIR/dU38b+wmo0m2q7MNVfSct967EKF49wow9RHyFMO8iD8fH93PYT9om5NdUha3dvkWnisKcfuO5pZY3LLXPAnuZT/VfqxJjmPqb98iepBfqFb6SpM/02IVSql81XKJEbMBc4zPHp/Uace6e4UDGsn/hPCVsqQsTzrbKCR+ovpkhXipWwTYSlgsLe/o43k0UxhCN8eKhg835KuUkskA3T8C5Q6v6xgznlR7JJuhZpg==",
"MD5OfBody": "bbdc5fdb8be7251f5c910905db994bab",
"Body": "Information about current NY Times fiction bestseller for week of 12/11/2016.",
"Attributes": {
"SentTimestamp": "1553851566164"
},
"MD5OfMessageAttributes": "d25a6aea97eb8f585bfa92d314504a92",
"MessageAttributes": {
"Author": {
"StringValue": "John Grisham",
"DataType": "String"
},
"Title": {
"StringValue": "The Whistler",
"DataType": "String"
},
"WeeksOn": {
"StringValue": "6",
"DataType": "Number"
}
}
}
]
and here is my python code to check:
if 'Messages' in response:
message = response['Messages'][0]
receipt_handle = message['ReceiptHandle']
sqs.delete_message(
QueueUrl=queue_url,
ReceiptHandle=receipt_handle
)
print('Received and deleted message: %s' % message)
else:
print('Message not received yet')
Please let me know above code is good practice or not.
Messagesinresponse.0thelement in it and then find the key.