0

I have a requirement to reproduce a json, basically to run some tests on my code that is used to parse similar json in real-time.

This is my code snippet, event is generated from s3 PUT notification i.e. published to SQS from SNS listener -

event_body = json.loads(event["Records"][0]["body"])
event_body_msg = json.loads(event_body['Message'])
event_body_dict = event_body_msg['Records'][0]
s3_buck = event_body_dict['s3']['bucket']['name']

I want my json to contain the hierarchical structure that would be parsed by this code (as my test is written on the value of s3_buck). Here is the json I came up with -

{ "Records": [ { "messageId": "a6665910-ab5a-46c3-baaf-6086c0c90511", "receiptHandle": "AQEBscBCR7DwSLqd5SXvEAX+8NUImpMPNmJ9hSD03HgWHhPnNZoIIqHkqI8lvwGMLjhX2R1ogPfo09z8EHcI7Nuh851vi4cIPBngMbIS6yw/rBtG115vSUyfN8i1yKM6Oz7iSJ2kIJCGmWRF2Rhsc8dH31zcyZKbVz/SzCOK8S/E9SdFHkPi2iNm4tr4PgrI+ZrvtYUicOuZQAJ8++hYo0rB43YCZKSZWMV1LG4iz2+OKVO08qZv3WyJ3pUegW4LXNp1xAf2abep44lYgWqqDWyWRlnpKayagqaTSaqR/OzNM3Iky9MnXqVz3g7CRBO28h2noUy4T6cW6HmlZ+xe3TWHOToJeWqiRnsY1HYuZxGscRpDUXIq5V7pZPhkLU2XbdQg", "body": "{\"Message\": {\"Records\": [{\"s3\": {\"bucket\": {\"name\": \"demo-bucket-name\",\"arn\": \"arn:aws:s3:::demo-bucket-name\"},\"object\": {\"key\": \"demo-key-prefix.json\"}}}]}}" } ] }

I am trying to replicate aws sns notification to create a sample json that would only contain attributes for my usecase. Here is the sample event sns produces(copied from the lambda console) - https://www.heypasteit.com/clip/0IUAE3. I am only picking attributes that I need like s3 bucket_name or arn etc.

However, the problem here is that I run into errors where I run event["Records"][0]["body"], with the error message -

Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/init.py", line 354, in loads return _default_decoder.decode(s) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 342, in decode raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 1 column 251 (char 250)

I tried enclosing the json string inside "body" key using r""" but no luck. Wondering what's the right format to create the json.

1 Answer 1

1

body is not valid as a JSON string. It contains 2 records without [ and ,.

Let's print your body string then validate with https://jsonlint.com/

Sign up to request clarification or add additional context in comments.

3 Comments

Ah..fixed that(in the question as well), now it succeeds to json.loads() and create event_body. However, the content inside with "Records" is still identified as dictionary (and hence loads() fails). I tried adding quotes to the "Records" dict but it then fails at the loads() to create event_body with the error message - "json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 16 (char 15)".
I think this is other question. But I still paste code here. print(json.loads(event["Records"][0]["body"])["Message"]["Records"][0]["s3"]["bucket"]["name"])
You should not update question by remove old messages. Thanks

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.