Pushing nested json into influxdb using python

json data =
{
“data”: {
“timelines”: [
{
“timestep”: “current”,
“endTime”: “2022-10-12T00:21:00Z”,
“startTime”: “2022-10-12T00:21:00Z”,
“intervals”: [
{
“startTime”: “2022-10-12T00:21:00Z”,
“values”: {
“humidity”: 62,
“temperature”: 20.88,
“windSpeed”: 7
}
}
]
}
]
}
}

#######################
from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
from datetime import datetime
import json

jsonlocation = open(‘weather.json’,‘r’)
#appversionVal = 1.30

returns JSON object as

a dictionary

dataweather = json.load(jsonlocation)
JsonDataInflux = {“measurement”: “weather”,
“tags”: {
#“appversion”: appversionVal
}, “fields”: dataweather[“data”]}

print(JsonDataInflux)

token = token
org = org
bucket = bucket

with InfluxDBClient(url=“http://localhost:8086”, token=token, org=org) as client:
write_api = client.write_api(write_options=SYNCHRONOUS)

write_api.write(bucket=bucket, record=JsonDataInflux)
client.close()

###############
how to i push the json using the fields?

Hi @Aakash_Shrestha,

thanks for using our client.

You can use something like:

import json

from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS

url = "http://localhost:8086"
token = "my-token"
org = "my-org"
bucket = "my-bucket"

app_version = 1.30
with open('weather.json.txt', 'r') as file:
    json_data = json.load(file)

with InfluxDBClient(url=url, token=token, org=org, default_tags={'appversion': str(app_version)}, debug=False) as client:
    def flatten_interval(interval):
        values = interval['values']
        values['startTime'] = interval['startTime']
        return values


    # get timelines
    timelines = json_data['data']['timelines']
    # get intervals
    intervals = list(map(lambda timeline: timeline['intervals'], timelines))
    # flatten intervals
    intervals = [flatten_interval(interval) for sublist in intervals for interval in sublist]
    
    # write data
    client.write_api(write_options=SYNCHRONOUS).write(bucket=bucket,
                                                      record=intervals,
                                                      record_measurement_name="weather",
                                                      record_time_key="startTime",
                                                      record_field_keys=["humidity", "temperature", "windSpeed"])

weather.json.txt (418 Bytes)

Regards

1 Like

Hi @bednar
Thank you for your response.
I used the code you suggested, but the database seems to be empty at first. Later i pulled new data into the weather.json file and ran the script. it showed the following error.

Traceback (most recent call last):
File “c:\Users\akash\internship\database.py”, line 36, in
client.write_api(write_options=SYNCHRONOUS).write(bucket=bucket,
File “C:\Users\akash\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\influxdb_client\client\write_api.py”, line 374, in write
results = list(map(write_payload, payloads.items()))
File “C:\Users\akash\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\influxdb_client\client\write_api.py”, line 372, in write_payload
return self._post_write(_async_req, bucket, org, final_string, payload[0])
File “C:\Users\akash\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\influxdb_client\client\write_api.py”, line 483, in _post_write
return self._write_service.post_write(org=org, bucket=bucket, body=body, precision=precision,
File "C:\Users\akash\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\influxdb_client\service\write_se File “C:\Users\akash\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\influxdb_client_sync\api_client.py”, line 388, in request
return self.rest_client.POST(url, File “C:\Users\akash\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\influxdb_client_sync\rest.py”, line 307, in POST
return self.request(“POST”, url, File “C:\Users\akash\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\influxdb_client_sync\rest.py”, line 257, in request
raise ApiException(http_resp=r)influxdb_client.rest.ApiException: (422)
Reason: Unprocessable Entity
HTTP response headers: HTTPHeaderDict({‘Content-Type’: ‘application/json; charset=utf-8’, ‘X-Influxdb-Build’: ‘OSS’, ‘X-Influxdb-Version’: ‘v2.4.0’, ‘X-Platform-Error-Code’: ‘unprocessable entity’, ‘Date’: ‘Wed, 12 Oct 2022 11:41:44 GMT’, ‘Content-Length’: ‘224’})
HTTP response body: {“code”:“unprocessable entity”,“message”:“failure writing points to database: partial write: field type conflict: input field "windSpeed" on measurement "weather" is type float, already exists as type integer dropped=1”}

Hi @bednar
i managed to change the data in the weather.json into the relevevant data type and it takes in the data when the data type are matched
“humidity”: 74,
“temperature”: 19.5,
“windSpeed”: 5

but fails to update the database when the data changes to
“humidity”: 74,
“temperature”: 19.5,
“windSpeed”: 5.5

@Aakash_Shrestha it is caused by different type for windSpeed:

  • 5 is integer
  • 5.5 is float

you have to pre-process your data to have same type. It is same problem as is reported here Add uint64 support to line protocol serializer · Issue #516 · influxdata/influxdb-client-python · GitHub