[英]Using JsonQuery in Python fails to fetch value in variable
I'm pretty new to using Python in AWS Lambda.... and sepent a few hours on this trying to fetch a value into varaiable using JSON. I'm basically making an external API call in my function http://informationhub.mocklab.io/consumption/tenantorganizations/v3 .我对在 AWS Lambda 中使用 Python 很陌生....并花了几个小时尝试使用 JSON 将值提取到 varaiable 中。我基本上是在我的 function 8825418274.informationhub://informationhub中进行外部 API 调用mocklab.io/consumption/tenantorganizations/v3 。 From the json returned, I get an error when trying to fetch the TenantId from the root item where "Acme Ltd" is in the customer name list.从返回的 json 中,我在尝试从客户名单中“Acme Ltd”所在的根项目中获取 TenantId 时收到错误消息。 "list indices must be integers or slices, not str" “列表索引必须是整数或切片,而不是 str”
My code:我的代码:
import json
import requests,boto3
def lambda_handler(event, context):
#CoName = event['CoName']
url = "http://informationhub.mocklab.io/consumption/tenantorganizations/v3"
response = requests.request("GET", url)
response = (response.text)
#print(response.text)
json_response = json.loads(response)
# the result is a Python dictionary:
TenId = (json_response["$[1].tenantId"])
print(TenId )
json.loads
transforms your request body in a list
of dictionaries
. json.loads
将您的请求正文转换为dictionaries
list
。 If you need the tenantId
of the first element, you have to retrieve the first element of the list and from there get the item based on the key tenantId
:如果您需要第一个元素的tenantId
,则必须检索列表的第一个元素并从那里根据键tenantId
获取项目:
import json
import requests
def lambda_handler(event, context):
url = "http://informationhub.mocklab.io/consumption/tenantorganizations/v3"
response = requests.request("GET", url)
json_response = json.loads(response.text)
print(json_response[0]['tenantId'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.