0

I'm struggling to access some values in this nested json in python.

How can I access this ['Records'][0]['s3']['bucket']['name'] ? I did search a lot to find a simple python snippet, but no luck. Thanks in advance!

{
  "Records": [
    {
      "eventName": "xxxxxxx",
      "userIdentity": {
        "principalId": "AWS:XXXXXXXXXXXXXX"
      },
      "requestParameters": {
        "sourceIPAddress": "XX.XX.XX.XX"
      },
      "responseElements": {
        "x-amz-request-id": "8CXXXXXXXXXXHRQX",
        "x-amz-id-2": "doZ3+gxxxxxxx"
      },
      "s3": {
        "s3SchemaVersion": "1.0",
        "configurationId": "X-Event",
        "bucket": {
          "name": "bucket-name",
          "ownerIdentity": {
            "principalId": "xxxxxxx"
          },
          "arn": "arn:aws:s3:::bucket-name"
        },
        "object": {
          "key": "object.png",
          "sequencer": "0060XXXXXXX75X"
        }
      }
    }
  ]
}
7
  • json.loads Commented Apr 5, 2021 at 2:47
  • @cosmos-1905-14 Is this a file or is it a JSON string? Commented Apr 5, 2021 at 2:52
  • @TheSupreme It's a string Commented Apr 5, 2021 at 2:54
  • Actually, I made a library for this a little while ago: QueryTree. It makes writing to deeply nested structures and converting them between formats. But if you are just trying to read that value, you already did it. The snipet you gave will work. Just parse the json first with the built-in json library Commented Apr 5, 2021 at 2:54
  • @cosmos-1905-14 I'm not sure exactly what you mean by nested but both the links I gave can absolutely handle the JSON structure in your question Commented Apr 5, 2021 at 2:58

2 Answers 2

2

Since this is a string, use the json.loads method from the inbuilt JSON library.

import json

json_string = # your json string
parsed_string = json.loads(json_string)
print(parsed_string) # it will be a python dict
print(parsed_string['Records'][0]['s3']['bucket']['name']) # prints the string
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried running your example? If you're loading the json from elsewhere, you'd need to convert it to this native dictionary object using the json library (as mentioned by others, json.loads(data))

kv = {
  "Records": [
    {
      "eventName": "xxxxxxx",
      "userIdentity": {
        "principalId": "AWS:XXXXXXXXXXXXXX"
      },
      "requestParameters": {
        "sourceIPAddress": "XX.XX.XX.XX"
      },
      "responseElements": {
        "x-amz-request-id": "8CXXXXXXXXXXHRQX",
        "x-amz-id-2": "doZ3+gxxxxxxx"
      },
      "s3": {
        "s3SchemaVersion": "1.0",
        "configurationId": "X-Event",
        "bucket": {
          "name": "bucket-name",
          "ownerIdentity": {
            "principalId": "xxxxxxx"
          },
          "arn": "arn:aws:s3:::bucket-name"
        },
        "object": {
          "key": "object.png",
          "sequencer": "0060XXXXXXX75X"
        }
      }
    }
  ]
}

print("RESULT:",kv['Records'][0]['s3']['bucket']['name'])

RESULT: bucket-name

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.