influxdata / influxdata/influxdb

client.write_points with JSON.dumps from python dict(dictionary) FAILED - solution

Open
#24,231 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
31.7k
Forks
3.7k
Avg merge
13h 37m
Merged PRs (30d)
8

Description

  •    add square brackets [ to JSON.dumps ] and
  •    then use JSON.loads() to get the expected type(list)/class
  • client.write_points( json.loads( '[' + json.dumps( python_dict ) + ']' ) ) WORKED

Hope this will save time for other influxDB newbies. The story behind ...
collected a data point structure into the python_dict/dictionary for InfluxDB

python_dict = dict( { 'measurement': 'eACstatus',
	'tags': { 'Name':"my" }, 
	'fields': {
	'L1Voltage_V':0.0,'L2Voltage_V':0.0,'L3Voltage_V':0.0,
	'GridFrequency_Hz':0.0,
	'L1Current_A':0.0,'L2Current_A':0.0,'L3Current_A':0.0
	} } )

client.write( json.dumps(python_dict) ) FAILED for two reasons

  • client.write expects JSON, but of type(list) "json is not JSON"
  • json.dumps() delivers type(str) and without square brackets

Tests with json_body constructed manually, which is of type(list), examples you find all over the web

json_body = [
       {
           "measurement": "eACstatus", 
           "tags": {
		"Name": "my"
           },
           "fields": {
		"L1Voltage_V": 0.0, "L2Voltage_V": 225.6999969482422, "L3Voltage_V": 0.0, 
		"GridFrequency_Hz": 50.040000915527344, 
		"L1Current_A": 0.0, "L2Current_A": 0.009999999776482582, "L3Current_A": 0.0
            }
       }
   ]

A) client.write_points( json_body ) which is of type(list) WORKED
type= <class 'list'>
[{'measurement': 'eACstatus', 'tags': {'Name': 'my'}, 'fields': {'L1Voltage_V': 0.0, 'L2Voltage_V': 225.6999969482422, 'L3Voltage_V': 0.0, 'GridFrequency_Hz': 50.040000915527344, 'L1Current_A': 0.0, 'L2Current_A': 0.009999999776482582, 'L3Current_A': 0.0}}]

B) client.write_points( json.dumps(json_body) ) which is of type(str) FAILED
type= <class 'str'>
[{"measurement": "eACstatus", "tags": {"Name": "my"}, "fields": {"L1Voltage_V": 0.0, "L2Voltage_V": 225.6999969482422, "L3Voltage_V": 0.0, "GridFrequency_Hz": 50.040000915527344, "L1Current_A": 0.0, "L2Current_A": 0.009999999776482582, "L3Current_A": 0.0}}]

Notice the only difference between A) and B), type= <class> that helped to find the solution for python_dict.

C) SOLUTION for python_dict,dictionary

to json.dumps(python_dict) ADD square brackets [], then use JSON.loads()

dict_dumps = json.dumps(python_dict) ### is also missing the square brackets

type= <class 'str'>
{"measurement": "eACstatus", "tags": {"Name": "6DL"}, "fields": {"L1Voltage_V": 0.0, "L2Voltage_V": 227.0, "L3Voltage_V": 0.0, "GridFrequency_Hz": 50.02000045776367, "L1Current_A": 0.0, "L2Current_A": 0.009999999776482582, "L3Current_A": 0.0}}

dict_body = '[' + json.dumps(python_dict) + ']' ### with added square brackets

type= <class 'str'>
[{"measurement": "eACstatus", "tags": {"Name": "6DL"}, "fields": {"L1Voltage_V": 0.0, "L2Voltage_V": 227.0, "L3Voltage_V": 0.0, "GridFrequency_Hz": 50.02000045776367, "L1Current_A": 0.0, "L2Current_A": 0.009999999776482582, "L3Current_A": 0.0}}]

dict_body_loads = json.loads( dict_body ) ### the desired type(list)

type= <class 'list'>
[{'measurement': 'eACstatus', 'tags': {'Name': '6DL'}, 'fields': {'L1Voltage_V': 0.0, 'L2Voltage_V': 227.0, 'L3Voltage_V': 0.0, 'GridFrequency_Hz': 50.02000045776367, 'L1Current_A': 0.0, 'L2Current_A': 0.009999999776482582, 'L3Current_A': 0.0}}]

client.write_points(dict_body_loads) ### gets WRITTEN to influxDB

influxDB 1.8.10 python 3.9.2 this solution is WORKING, sorry for posting here, it is not a bug, only a tricky obstacle.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue provides Python examples for client.write_points and JSON conversion, but names no repository file, test, or requested documentation change. Read the existing client.write_points documentation and related examples first; maintainer guidance is needed to define the intended update and what would count as done.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, databases
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.