0

I am trying to insert a json_body in the influxdb. But I am getting an error Unable to parse the data.Invalid format Error code 400.

Here is my code:

from influxdb import InfluxDBClient
import json, ast

def insertFeedData(feed, sensorType):
   client = InfluxDBClient(host='localhost', port=8086)
   write_command = 'mqtt_feed,feed=' + feed + ' sensor_type='+  str(sensorType) +',sensor_id=' + str(sensorType)
   print(write_command)
   client.write([write_command],{'db':'SmartRural_feed'},204,'line')
   client.close()


feed = '{"end_device_ids":{"device_id":"XXXXXXXXXXXXXXX","application_ids":{"application_id":"digitalmatter-oyster-01"},"dev_eui":"XXXXXXXXXXXXXXX","join_eui":"XXXXXXXXXXXXXXX","dev_addr":"XXXXXXXXX"},"correlation_ids":["as:up:XXXXXXXXXXXX","gs:conn:XXXXXXXXXXXXXX","gs:up:host:01F1C1P1F46RM6Q2H5GSBF9V7E","gs:uplink:01F1CCHXF6SBFA50HJCHPVAN7X","ns:uplink:XXXXXXXXXXXX","rpc:/ttn.lorawan.v3.GsNs/HandleUplink:01F1CCHXF8YQKMFRKMNWSKC6GK","rpc:/ttn.lorawan.v3.NsAs/HandleUplink:XXXXXXXXXXXXXXXX"],"received_at":"2021-03-22T07:14:29.051626349Z","uplink_message":{"session_key_id":"AXhFTz+fm9aPrK/fzAbGoQ==","f_port":1,"f_cnt":361,"frm_payload":"XXXXXXXXX","decoded_payload":{"aamsg_type":"position","battV":4.675,"fixok":True,"heading":0,"intrip":False,"lat":57.4510524,"lon":-2.9442034,"speedKmph":0},"rx_metadata":[{"gateway_ids":{"gateway_id":"05-0008","eui":"FCC23DFFFE20F3AC"},"timestamp":743687252,"rssi":-97,"channel_rssi":-97,"snr":9.5,"location":{"source":"SOURCE_REGISTRY"},"uplink_token":"XXXXXX","channel_index":7}],"settings":{"data_rate":{"lora":{"bandwidth":125000,"spreading_factor":7}},"data_rate_index":5,"coding_rate":"4/5","frequency":"867900000","timestamp":743687252},"received_at":"2021-03-22T07:14:28.460612285Z","consumed_airtime":"0.061696s"}}'

feed_dict = ast.literal_eval(feed)

insertFeedData(str(feed_dict), 2)

I am not sure what I am doing wrong here. I have also thought of inserting the data in this format.

json_body = [
        {
            "measurement": "mqtt_feed",
            "tags": {
                "host": "server01",
                "region": "us-west"
            },
            "time": "2009-11-10T23:00:00Z",
            "fields": {
                "value": 0.64
            }
        }
    ]

But here I am getting an error. Database is required. Though I have used client.write_database('db'). Can Any suggestions will be of great help. Thanks in advance.

1 Answer 1

1
client = InfluxDBClient(host='localhost', port=8086)
client.switch_database('db')#your db name
client.write_points(json_body)

Try and use the above code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.