# InfluxDB write api python: write list of dictionaries

**URL:** <https://community.influxdata.com/t/influxdb-write-api-python-write-list-of-dictionaries/38568>\
**Category:** Systems\
**Created:** [September 30, 2024, 8:26am UTC](https://community.influxdata.com/t/influxdb-write-api-python-write-list-of-dictionaries/38568 "2024-09-30T08:26:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lory\_96](https://avatars.discourse-cdn.com/v4/letter/l/b9bd4f/32.png) [@Lory\_96](https://community.influxdata.com/u/Lory_96)\
**Post date:** [September 30, 2024, 8:26am UTC](https://community.influxdata.com/t/influxdb-write-api-python-write-list-of-dictionaries/38568/1 "2024-09-30T08:26:35Z")

</div>

i want to use influxdb client library in python to send data to my bucket.

It’s an iot data coming from an api request (different machines with each different sensors).

So far, i managed to process the json response of API call and parsed the data i want to store as follows:  
for each machine i have a dictionary with variables(machine\_name\_sensor\_name) as keys and values (float). Whole data is stored in a list (list of dicts)

```auto
list_all_data=[
{"machine_name1_sensor_name1": value,
"machine_name1_sensor_name2" : value},
{"machine_name2_sesnor_name1": value,
"machine_name2_sensor_name2" : value}
]

```

How can i push this data to influxDB ? i’ve been trying to figure this out but it’s not clear. I want to have 1 measurement for each machine . i’m using InfluxDB v2.7.6

---

<div class="post-metadata">

**Author:** ![Lory\_96](https://avatars.discourse-cdn.com/v4/letter/l/b9bd4f/32.png) [@Lory\_96](https://community.influxdata.com/u/Lory_96)\
**Post date:** [September 30, 2024, 9:00am UTC](https://community.influxdata.com/t/influxdb-write-api-python-write-list-of-dictionaries/38568/2 "2024-09-30T09:00:57Z")

</div>

so i tried again starting with one machine, still no luck. list index out of range error

```auto
#set up client 
client = InfluxDBClient(
	url=url,
	org=org,
	token=token,
	bucket=bucket,
	verify_ssl=False
)

write_api= client.write_api(write_options=SYNCHRONOUS)

machine_name="Oven"
sensor_names=["TC1","TC2"]
sensor_values=[14,125]

#conrtsuct point structure as influxdb expects
for i ,j in enumerate(sensor_names):
    for k,l in enumerate(sensor_values):
         payload = {
                "measurement" : machine_name,
                "tags": {"variable" :sensor_name1[i]}, # expects : tags :{"variable":"TC1"}
		        "fields": {sensor_names[i] : sensor_values[k]}, # expects fields :{"TC1":14}
		}
	data.append(payload)

write_api.write(bucket=bucket, org=org, record=data)

```

---

<div class="post-metadata">

**Author:** ![Lory\_96](https://avatars.discourse-cdn.com/v4/letter/l/b9bd4f/32.png) [@Lory\_96](https://community.influxdata.com/u/Lory_96)\
**Post date:** [September 30, 2024, 1:39pm UTC](https://community.influxdata.com/t/influxdb-write-api-python-write-list-of-dictionaries/38568/3 "2024-09-30T13:39:32Z")

</div>

0

i have a list of dictionaries that i want to process so that it matchs influxDB dict format:

```auto
dict={
"measurement": 'measurement_name",
"tags" : {"tag_key":"tag_value"},
"fields : {"field_name" : field_value}
}

```

my list of dicts:

```auto
list_val_machines=[
{"machine1_Data_variable1": value,
"machine1_Data_variable2" : value},
{"machine2_Data_variable1": value,
"machine1_Data_variable2" : value}
]

```

so here is what i tried so far:

```auto
for k,v in enumerate(list_val_machines): #loop on list items 
    
    for i,j in v.items(): #iterate on every key value of each dict 
        #list_machine_names=[]
        #for k,v in raw_data_dict.items():
        #list_machine_names.append(machine_name)
        machine_name=i.split('_')[0] #parse machine name 
        variable_names=list(v.keys()) #get variable names (tag keys)
        values_name=list(v.values()) #get values (field values)
        for l,m in enumerate(variable_names):
            for n,o in enumerate(values_name):
                d={
                "measurement" : machine_name,
                "tags" : {"variable_name": m},
                "fields" : {"variable_name": o}     
                }
        list_d.append(d)
data=list_d

data variable looks like this: 
for p in data:
  print(p)

output:

{'measurement': 'machine1', 'tags': {'variable_name': 'machine1_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine1', 'tags': {'variable_name': 'machine1_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine1', 'tags': {'variable_name': 'machine1_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine1', 'tags': {'variable_name': 'machine1_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine2', 'tags': {'variable_name': 'machine2_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine2', 'tags': {'variable_name': 'machine2_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine2', 'tags': {'variable_name': 'machine2_Data_variable1'}, 'fields': {'variable_name': 0.0}}

```

measurement name is correct but i’m having the same tag field for every machine (exemple: machine1\_Data\_variable1) and same field value for every variable in a given machine. I don’t understand what is going wrong here. I want to have a 1 dict per variable with correct variable names and value.

Any hints on how to achieve this ?

---

<div class="post-metadata">

**Author:** ![suyash](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/suyash/32/15791_2.png) [@suyash](https://community.influxdata.com/u/suyash)\
**Post date:** [October 1, 2024, 5:01pm UTC](https://community.influxdata.com/t/influxdb-write-api-python-write-list-of-dictionaries/38568/4 "2024-10-01T17:01:21Z")

</div>

It seems the problem in your current logic is that you’re trying to process the dictionary as a whole for each key-value pair, rather than processing each key-value pair individually.
