简体   繁体   English

在 Python 中使用 JsonQuery 无法获取变量中的值

[英]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.

相关问题 使用 python 检查 dynamodb 表中是否存在值并获取该记录 - check if a value exists in dynamodb table using python and fetch that record 无法获取 Terraform 模块中定义的变量值 - Not able to fetch the variable's value defined in a module in Terraform 如何使用 javascript 从 firebase 中的键中获取值 - How do I fetch the value from a key in firebase using javascript 从 lambda 连接到 Redshift 并使用 python 获取一些记录 - connect to Redshift from lambda and fetch some record using python 使用 Fetch 删除用户端点 - Delete user endpoint using Fetch 从 SharePoint JSON output 获取值 - fetch value from SharePoint JSON output 使用 Python 检查 FireBase 数据库中是否存在特定值 - Check If a Particular Value Exists in the FireBase Database Using Python 我如何使用一种方法从 Firebase 实时数据库中获取单个值 - How can i fetch single value from Firebase real time database using a method FIrebase 或对象/数组 - 如何在不使用变量值的情况下将数字/值添加到变量? - FIrebase or object/Array- how to add a number/value to a variable without using the variable value? 无法使用外部数据源获取数据 - Unable to fetch data using external data source
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM