[英]check if a value exists in dynamodb table using python and fetch that record
[英]Using JsonQuery in Python fails to fetch value in variable
我对在 AWS Lambda 中使用 Python 很陌生....并花了几个小时尝试使用 JSON 将值提取到 varaiable 中。我基本上是在我的 function 8825418274.informationhub://informationhub中进行外部 API 调用mocklab.io/consumption/tenantorganizations/v3 。 从返回的 json 中,我在尝试从客户名单中“Acme Ltd”所在的根项目中获取 TenantId 时收到错误消息。 “列表索引必须是整数或切片,而不是 str”
我的代码:
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
将您的请求正文转换为dictionaries
list
。 如果您需要第一个元素的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.