Skip to content

how to write JSON to influxdb cloud using python #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Mogoatlhe opened this issue Jun 4, 2020 · 2 comments · Fixed by #105
Closed

how to write JSON to influxdb cloud using python #103

Mogoatlhe opened this issue Jun 4, 2020 · 2 comments · Fixed by #105
Labels
bug Something isn't working
Milestone

Comments

@Mogoatlhe
Copy link

Hi, i am having trouble sending data to my influxdb cloud instance in JSON format using Python 3.6.9.

body = Point("my_measurement").tag("location", "Prague").tag("location", "Tladistad").field("temperature", 25.3) write_api.write(bucket=buck, org=organisation, record=body)

the above works fine, but i want to use json (dictionary) and i also want have multiple tags and fields like below

json_body = [{"measurement": organisation, "tags":{"tag1":"tag1", "tag2": "tag2"}, "fields":{'field1':1, "field2":2}}] write_api.write(bucket=buck, org=organisation, record=json_body)

i get the error below

Traceback (most recent call last): File "initialize.py", line 12, in <module> write_api.write(bucket=buck, org=organisation, record=json_body) File "/home/moeketsi/.local/lib/python3.6/site-packages/influxdb_client/client/write_api.py", line 217, in write final_string = self._serialize(record, write_precision, **kwargs) File "/home/moeketsi/.local/lib/python3.6/site-packages/influxdb_client/client/write_api.py", line 263, in _serialize **kwargs) for item in record]) File "/home/moeketsi/.local/lib/python3.6/site-packages/influxdb_client/client/write_api.py", line 263, in <listcomp> **kwargs) for item in record]) File "/home/moeketsi/.local/lib/python3.6/site-packages/influxdb_client/client/write_api.py", line 253, in _serialize _result = self._serialize(Point.from_dict(record, write_precision=write_precision), File "/home/moeketsi/.local/lib/python3.6/site-packages/influxdb_client/client/write/point.py", line 37, in from_dict point.time(dictionary['time'], write_precision=write_precision) KeyError: 'time'

can you kindly assist, thank you (edited)

@bednar bednar added the bug Something isn't working label Jun 5, 2020
@bednar bednar added this to the 1.8.0 milestone Jun 5, 2020
@bednar
Copy link
Contributor

bednar commented Jun 5, 2020

Hi @Mogoatlhe,

It was caused by bug in:

def from_dict(dictionary: dict, write_precision: WritePrecision = DEFAULT_WRITE_PRECISION):

The issue is fixed in 1.8.0 milestone.

As workaround you could use something like:

json_body =  [{"measurement": organisation, "tags":{"tag1":"tag1", "tag2": "tag2"}, "fields":{'field1':1, "field2":2}, "time": None}]
write_api.write(bucket=buck, org=organisation, record=json_body)

or if you would like to use a dev version then install client via:

pip install git+https://github.com/influxdata/influxdb-client-python.git@master

Regards

@Mogoatlhe
Copy link
Author

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants