user3562335
user3562335

Reputation: 1

Creating JSON data from string and using json.dumps

I am trying to create JSON data to pass to InfluxDB. I create it using strings but I get errors. What am I doing wrong. I am using json.dumps as has been suggested in various posts.

Here is basic Python code:

json_body = "[{'points':["
json_body += "['appx', 1, 10, 0]"
json_body += "], 'name': 'WS1', 'columns': ['RName', 'RIn', 'SIn', 'OIn']}]"

print("Write points: {0}".format(json_body))
client.write_points(json.dumps(json_body))

The output I get is

Write points: [{'points':[['appx', 1, 10, 0]], 'name': 'WS1', 'columns': ['RName', 'RIn', 'SIn', 'OIn']}]
Traceback (most recent call last):

line 127, in main
    client.write_points(json.dumps(json_body))
  File "/usr/local/lib/python2.7/dist-packages/influxdb/client.py", line 173, in write_points
    return self.write_points_with_precision(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/influxdb/client.py", line 197, in write_points_with_precision
    status_code=200
  File "/usr/local/lib/python2.7/dist-packages/influxdb/client.py", line 127, in request
    raise error
influxdb.client.InfluxDBClientError

I have tried with double quotes too but get the same error. This is stub code (to minimize the solution), I realize in the example the points list contains just one list object but in reality it contains multiple. I am generating the JSON code reading through outputs of various API calls.

json_body = '[{\"points\":['
json_body += '[\"appx\", 1, 10, 0]'
json_body += '], \"name\": \"WS1\", \"columns\": [\"RName\", \"RIn\", \"SIn\", \"OIn\"]}]'

print("Write points: {0}".format(json_body))
client.write_points(json.dumps(json_body))

I understand if I used the below things would work:

json_body = [{ "points": [["appx", 1, 10, 0]], "name": "WS1", "columns": ["Rname", "RIn", "SIn", "OIn"]}]

Upvotes: 0

Views: 2762

Answers (2)

Andrey Hitrin
Andrey Hitrin

Reputation: 11

You don't need to create JSON manually. Just pass an appropriate Python structure into write_points function. Try something like that:

data = [{'points':[['appx', 1, 10, 0]],
         'name': 'WS1',
         'columns': ['RName', 'RIn', 'SIn', 'OIn']}]

client.write_points(data)

Upvotes: 1

MattDMo
MattDMo

Reputation: 102842

Please visit JSON.org for proper JSON structure. I can see several errors with your self-generated JSON:

  1. The outer-most item can be an unordered object, enclosed by curly braces {}, or an ordered array, enclosed by brackets []. Don't use both. Since your data is structured like a dict, the curly braces are appropriate.
  2. All strings need to be enclosed in double quotes, not single. "This is valid JSON". 'This is not valid'.
  3. Your 'points' value array is surrounded by double brackets, which is unnecessary. Only use a single set.

Please check out the documentation of the json module for details on how to use it. Basically, you can feed json.dumps() your Python data structure, and it will output it as valid JSON.

In [1]: my_data = {'points': ["appx", 1, 10, 0], 'name': "WS1", 'columns': ["RName", "RIn", "SIn", "OIn"]}

In [2]: my_data
Out[2]: {'points': ['appx', 1, 10, 0], 'name': 'WS1', 'columns': ['RName', 'RIn', 'SIn', 'OIn']}

In [3]: import json

In [4]: json.dumps(my_data)
Out[4]: '{"points": ["appx", 1, 10, 0], "name": "WS1", "columns": ["RName", "RIn", "SIn", "OIn"]}'

You'll notice the value of using a Python data structure first: because it's Python, you don't need to worry about single vs. double quotes, json.dumps() will automatically convert them. However, building a string with embedded single quotes leads to this:

In [5]: op_json = "[{'points':[['appx', 1, 10, 0]], 'name': 'WS1', 'columns': ['RName', 'RIn', 'SIn', 'OIn']}]"

In [6]: json.dumps(op_json)
Out[6]: '"[{\'points\':[[\'appx\', 1, 10, 0]], \'name\': \'WS1\', \'columns\': [\'RName\', \'RIn\', \'SIn\', \'OIn\']}]"'

since you fed the string to json.dumps(), not the data structure.

So next time, don't attempt to build JSON yourself, rely on the dedicated module to do it.

Upvotes: 0

Related Questions