[英]Extracting from Json
From one of the method i am getting below output从我低于输出的方法之一
{'Records': [{'messageId': '2953dfd5-d848-42b2-a60b-43df00ec8e5f',
'receiptHandle': 'AQEBPMr5RbW3T2DG4pAYi+', 'body':
'I am still trying', 'attributes': {'ApproximateReceiveCount': '1',
'SentTimestamp': '1552073955807', 'SenderId': '944198216610',
'ApproximateFirstReceiveTimestamp': '1552073955816'},
'messageAttributes': {}, 'md5OfBody':
'2111a742ddbdac2d862fa6a204f7dc85', 'eventSource': 'aws:sqs',
'eventSourceARN': 'arn:aws:sqs:us-east-
1:944198216610:LambadaQueue', 'awsRegion': 'us-east-1'}]}
Now i want to fetch the value of body from this so i have used below现在我想从中获取 body 的值,所以我在下面使用了
body=event['Records'][0][0]['body'] body=event['Records'][0][0]['body']
But this is not working.Can you please help me figure out what wrong i am doing?但这不起作用。你能帮我弄清楚我在做什么错吗?
What am I doing wrong? 我究竟做错了什么?
The Records
key is a list and you can select items from a list using the index number for that item. “ Records
键是一个列表,您可以使用该项目的索引号从列表中选择项目。
json_string = {
"Records": [
{
"messageId": "2953dfd5-d848-42b2-a60b-43df00ec8e5f",
"receiptHandle": "AQEBPMr5RbW3T2DG4pAYi+",
"body": "I am still trying",
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1552073955807",
"SenderId": "944198216610",
"ApproximateFirstReceiveTimestamp": "1552073955816"
},
"messageAttributes": { },
"md5OfBody": "2111a742ddbdac2d862fa6a204f7dc85",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-1:944198216610: LambadaQueue",
"awsRegion": "us-east-1"
}
]
}
So, when you do json_string['Records'][0]
, this selects the first item in the list which is again a dictionary: 因此,当您执行json_string['Records'][0]
,这将选择列表中的第一项,它也是字典:
{
"messageId": "2953dfd5-d848-42b2-a60b-43df00ec8e5f",
"receiptHandle": "AQEBPMr5RbW3T2DG4pAYi+",
"body": "I am still trying",
....}
Now if you do json_string['Records'][0][0]
, you are trying to access a dictionary key like an item in a list(using index number 0) which is syntacticaly incorrect. 现在,如果您执行json_string['Records'][0][0]
,则尝试访问字典键,例如列表中的项(使用索引号0),这在语法上是不正确的。 You can access the key by name such as json_string['Records'][0]['messageId']
if you want to access the value for 'messageId', or as in your question, the "body" key's value like this: 如果要访问'messageId'的值,则可以按名称访问键,例如json_string['Records'][0]['messageId']
,或者在您的问题中,像这样输入“ body”键的值:
`json_string['Records'][0]['body']`
#Output:
I am still trying
Are you trying to get 'I am still trying'? 您是否要获得“我还在尝试”?
json_data = {
'Records': [{
'messageId': '2953dfd5-d848-42b2-a60b-43df00ec8e5f',
'receiptHandle': 'AQEBPMr5RbW3T2DG4pAYi+',
'body': 'I am still trying',
'attributes': {
'ApproximateReceiveCount': '1',
'SentTimestamp': '1552073955807',
'SenderId': '944198216610',
'ApproximateFirstReceiveTimestamp': '1552073955816'
},
'messageAttributes': {},
'md5OfBody': '2111a742ddbdac2d862fa6a204f7dc85',
'eventSource': 'aws:sqs',
'eventSourceARN': 'arn:aws:sqs:us-east-1: 944198216610: LambadaQueue',
'awsRegion': 'us - east - 1'
}]
}
print (json_data['Records'][0]['body'])
# output
# I am still trying
If you're attempting to get the value of the "body" element, it looks like you should just skip the second [0]
in your lookup. 如果您尝试获取“ body”元素的值,则似乎应该跳过查找中的第二个[0]
。 Properly formatted, it looks like this: 格式正确,如下所示:
{
"Records": [
{
"messageId": "2953dfd5-d848-42b2-a60b-43df00ec8e5f",
"receiptHandle": "AQEBPMr5RbW3T2DG4pAYi+",
"body": "I am still trying",
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1552073955807",
"SenderId": "944198216610",
"ApproximateFirstReceiveTimestamp": "1552073955816"
},
"messageAttributes": { },
"md5OfBody": "2111a742ddbdac2d862fa6a204f7dc85",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-1:944198216610: LambadaQueue",
"awsRegion": "us-east-1"
}
]
}
So, to get the value of the field "body" for the first record in "Records", it looks like you should do: body=event['Records'][0]['body']
因此,要获取“记录”中第一条记录的字段“正文”的值,您应该这样做: body=event['Records'][0]['body']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.