首页 > 解决方案 > 无法将 JSON 放入 InfluxDB

问题描述

试图将数据放入 InfluxDB 时,我感到很沮丧。我该怎么做呢?我的数据如下所示:

fields = {'value': 1, 'value2': 2, 'value3': 3}

我想为它添加一个日期时间戳,所以我这样设置它:

json_body = [
    {
        "timestamp": "2018-10-30",
        "fields": fields
    }
]

然后我用

client.write_points(json_body, database='database')

这给了我一个错误Unable to Parse : missing fields。我也尝试了很多不同的东西,例如:

json_body = {}
json_body["timestamp"] = "2018-10-30"
json_body["fields"] = fields
client.write_points(json_body, database='database') # returns 'str' has no obj attribute 'get'
client.write_points([json_body], database='database') # returns unable to parse : missing fields

有人可以指出我做错了什么以及如何解决吗?

标签: pythonjsoninfluxdb

解决方案


JSON 使用双引号。因此,您需要将字段字典中的单引号更改为双引号。

字段= {'value':1,'value2':2,'value3':3}

改成

字段 = {“值”:1,“值 2”:2,“值 3”:3}

为此,您需要:

import json

#Your code to create json_body

client.write_points(json.dumps(json_body), database='database')

推荐阅读