-1

I am trying to get key value from below unicode JSON is python

messagejson={
u'Records': 
[
{
u'requestParameters': {u'sourceIPAddress': u'113.112.10.06'}, 
u'sql': {u'configurationId': u'note', 
u'object': {u'eTag': u'ed3645fa5ee', u'sequencer': u'005', u'key': u'src-1.txt', u'size': 606}, 
u'mysql': {u'dn': u'url', u'name': u'invoice',
u'ownerIdentity': {u'principalId': u'A3UJ54'}
}, 
u'myval': u'1.0'
}, 
u'ee': {u'abc': u'tcCh6T', u'x': u'487DBE36E1'}, 
u'userIdentity': {u'principalId': u'AWS:AIDAIOJ4'}, 
u'eventSource': u'source'
}
]
}

Tried by using

print messagejson['Records'][0]['sql']['key']

getting error

TypeError: list indices must be integers, not str

can someone help

dfundako
  • 8,022
  • 3
  • 18
  • 34
Charan
  • 46
  • 1
  • 10

2 Answers2

2

You are missing one level in your dict get.

run this and you will see.

print messagejson['Records'][0]['sql']

The 'key' is within the value of the 'object' key, so you need to add 'object' before you get 'key'

print messagejson['Records'][0]['sql']['object']['key']
dfundako
  • 8,022
  • 3
  • 18
  • 34
1

You're missing the object level in your indexing:

messagejson['Records'][0]['sql']['object']['key']
# src-1.txt
andrew_reece
  • 20,390
  • 3
  • 33
  • 58