JSON无法正确保存

时间:2019-03-07 17:51:36

标签: python json python-3.x api

因此,我要从API中提取数据,并且只想保存特定的字典和JSON响应中的列表。问题是,当我将数据转储到循环中时,它会在文件中创建看起来很奇怪的数据,而实际上不是JSON。

r=requests.get(url,headers=header)
result=r.json()
with open ('myfile.json','a+') as file:
    for log in result['logs']:
        hello=json.dump(log['log']['driver']['username'], file)
        hello=json.dump(log['log']['driver']['first_name'],file)
        hello=json.dump(log['log']['driver']['last_name'],file)
        for event in log['log']['events']:
            hello=json.dump(event['event']['id'],file)
            hello=json.dump(event['event']['start_time'],file)
            hello=json.dump(event['event']['type'],file)
            hello=json.dump(event['event']['location'],file)

此处的最终目标是将这些数据转换为CSV。我将其保存到JSON文件的唯一原因是可以加载它并将其保存到CSV。我定位的API端点是日志:

https://developer.keeptruckin.com/reference#get-logs

2 个答案:

答案 0 :(得分:3)

我认为@GBrandt就创建有效的JSON输出而言是正确的主意,但是正如我在评论中说的那样,我认为JSON到JSON转换步骤不是真正必要的-因为您可以创建您已经拥有的JSON中的CSV文件:

(根据您的后续问题,也将start_time分为两个单独的字段。)

result = r.json()

with open('myfile.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile, quoting=csv.QUOTE_ALL)
    for log in result['logs']:
        username = log['log']['driver']['username']
        first_name = log['log']['driver']['first_name']
        last_name = log['log']['driver']['last_name']

        for event in log['log']['events']:
            id = event['event']['id']
            start_time = event['event']['start_time']
            date, time = start_time.split('T')  # Split time into two fields.
            _type = event['event']['type']  # Avoid using name of built-in.
            location = event['event']['location']
            if not location:
                location = "N/A"
            writer.writerow(
                (username, first_name, last_name, id, date, time, _type, location))

答案 1 :(得分:2)

您似乎只是以一种非结构化的方式将单个JSON字符串转储到文件中。

json.dump不会神奇地创建类似JSON dict的对象并将其保存到文件中。参见:

json.dump(log['log']['driver']['username'], file)

实际上它只是将驱动程序的用户名字符串化,然后直接将其转储到文件中,因此该文件将只有一个字符串,而不是JSON对象(我想这就是您想要的)。它是 JSON,只是没什么用。

您要寻找的是这个

r=requests.get(url,headers=header)
result=r.json()
with open ('myfile.json','w+') as file:
    logs = []
    for log in result['logs']:
        logs.append({
            'username': log['log']['driver']['username'],
            'first_name': log['log']['driver']['first_name'],
            'last_name': log['log']['driver']['last_name'],
            # ...
            'events': [
                ({
                    'id': event['event']['id'],
                    'start_time': event['event']['start_time'],
                    # ...
                }) for event in log['log']['events']
            ]
        })
    json.dump(logs, file)

此外,我建议不要在JSON文件上使用附加模式,.json应该包含一个JSON对象(就我而言)。