python - json_标准化: AttributeError list obj has no attribute 'value'

标签 python json api python-requests normalize

我正在处理来自 API 响应的 JSON 数据。我正在使用 python 发送带有地址数据的 GET 请求。我想捕获 JSON 响应并转换为数据帧。

目前,我在字典列表中捕获响应。我可以在 r.json 或sample_list[0] 上成功使用 json_normalize,但无法规范化整个列表。我试图避免在循环中创建和附加 df 以提高性能。

sample_list = []
for index, row in sample_df.iterrows():
    sample_address = json.dumps(
        {       
            "records": [
                {
                    "attributes": {
                    "OBJECTID": row['OBJECTID'],
                    "Address": row['Address'],
                    "City": row['City'],
                    "Region": row['Region'],
                    "Postal": row['Postal']
                }
                }
            ]
        }
        )

    r = re.get(url, params = { 'addresses': sample_address, 'f':'pjson'},verify = False)

    sample_list.append(r.json()['locations'])

###The Output of r.json for one address

{'locations': [{'address': '2600 Benjamin Franklin Pkwy, Philadelphia, Pennsylvania, 19130',..., 'score': 100}], 'spatialReference': {'latestWkid': 4326, 'wkid': 4326}}

###The sample_list of multiple r.json output
[[{'address': '520 Chestnut St, Philadelphia, Pennsylvania, 19106',
   'attributes': {'AddNum': '520',
   ...},
   'location': {'x': -75.14971142634045, 'y': 39.94905972672609},
   'score': 100}],
 [{'address': '2600 Benjamin Franklin Pkwy, Philadelphia, Pennsylvania, 19130',
   'attributes': {'AddNum': '2600',
    ...,
   'location': {'x': -75.17923104567541, 'y': 39.96474536190999},
   'score': 100}]]

最佳答案

在sample_list.append(r.json()['locations'])中只需尝试'json_normalize'而不是'r.json'。

关于python - json_标准化: AttributeError list obj has no attribute 'value' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59602312/

相关文章:

java - @JsonIgnoreProperties 用于参数的动态数量

python - 选项卡中的小部件放置

使用pytz的python时区转换问题

javascript - 如何在 Ajax $.getJSON() 中获取 Json 作为键和值?

json - 由于数据类型不匹配而获取 : argument 2 requires integral type error while parsing Json data Spark SQL

delphi - Adobe Acrobat API-如何跳过打开受密码保护的PDF?

javascript - 如何在 Postman 测试中更改用户名密码

java - 我想从 Java 中的 REST API 以 XML 形式返回响应

python - list[1] 和 list[1 :] in Python? 有什么区别

python - python 中的多个条