通过python将Json从http加载到postgres

问题描述 投票:0回答:1

我有一个由json数据组成的链接,如下所示:

http://www.bom.gov.au/fwo/IDQ60801/IDQ60801.94182.json

来自链接的示例数据:

"header": [
        {
            "refresh_message": "Issued at 12:02 pm EST Thursday  1 March 2018",
            "ID": "IDQ60801",
            "main_ID": "IDQ60800",
            "name": "Coconut Island",
            "state_time_zone": "QLD",
            "time_zone": "EST",
            "product_name": "Weather Observations",
            "state": "Queensland"
        }
    ],

如何通过python将上述数据加载到postgres表中?我应该先将它加载到数据框中,然后再将其加载到postgres中吗?我是数据框架和熊猫的新手,所以我想寻求你的帮助。

我在postgres中的预期表格如下所示:

ID      | main_id|     name     | state_timezone | time_zone |   product_name     |     state
IDQ60801|IDQ60800|Coconut Island|       QLD      |    EST    |Weather Observations| Queensland

感谢您的帮助

python json postgresql pandas dataframe
1个回答
0
投票

跳过数据帧和pandas。

使用Python的json模块来解析数据。这将以字典的形式提供数据。

import json
import requests

json_response = requests.get('http://www.bom.gov.au/fwo/IDQ60801/IDQ60801.94182.json')
json_content = json.loads(json_response.content)

# navigate the json, which are nested lists of dicts
# this below gives you the first, and only, header-dict
header_dict = json_content['observations']['header'][0]

将您的数据放在字典中,您可以按照这个很好的答案来了解如何将其导入数据库:Insert Python Dictionary using Psycopg2

© www.soinside.com 2019 - 2024. All rights reserved.