Adding Json Data from Python to Influxdb

Hi I m new to influx db . i do had studied documentation and view some youtube videos but not yet not understood the usage.

My Question to all you mentors is how to i store data from Json format in influx db from python
my Json format
{ “Stock”: “AAPL”,“TimeStamp”:10:00:00,“Open”: 150,“High”:154,“Low”:147,“Close”:141,“Volume”:3000}
{ “Stock”: “GE”,“TimeStamp”:10:00:00,“Open”: 150,“High”:154,“Low”:147,“Close”:141,“Volume”:3000}
{ “Stock”: “GS”,“TimeStamp”:10:00:00,“Open”: 150,“High”:154,“Low”:147,“Close”:141,“Volume”:3000}
{ “Stock”: “BOM”,“TimeStamp”:10:00:00,“Open”: 150,“High”:154,“Low”:147,“Close”:141,“Volume”:3000}
{ “Stock”: “XOM”,“TimeStamp”:10:00:00,“Open”: 150,“High”:154,“Low”:147,“Close”:141,“Volume”:3000}
i would like to store this one minute data into influx stock_tick Data
any kind of help will be greatly appreciated… thanks in advance

I had created database :

Create database tickdata
use tickdata

Measurement:
Exchange = “DOW”

Tags:
StockName= “GE"
StockType =” FUT" /“EQ”

Fields:
open
high
low
close
volume
open interest

TimeStamp:
09:00:01

If I am understanding you correctly, you just need to convert the JSON to InfluxDB Line Protocol in your script and output that format. You can then run that script via Telegraf to send it to InfluxDB.

#!/usr/bin/env python

from influxdb import InfluxDBClient

data = [
{
“measurement”: MEASUREMENT,
“tags”: {
TAGS
},
“time”: str(datetime.datetime.now()),
“fields”: {
FIELDS
}
}
]

client = InfluxDBClient(host='INFLUXHOST, port=INFLUXPORT, database=DBNAME)
client.write_points(data)

(just ident the lines as python wise)

I know thread is old but since I faced the same issue, here’s my solution:
I transformed the dicts to strings. So
{ “Stock”: “AAPL”,“TimeStamp”:10:00:00,“Open”: 150,“High”:154,“Low”:147,“Close”:141,“Volume”:3000}
becomes:
Stock=AAPL,Timestamp=10:00,Open=150,High=154,Low=147,Close=141,Volume=3000
then created a list of strings
and finally forged influxdb-fiendly string from the list with:
new-data = “\n”.join(mylist)
and then with python’s requst (for example) just added as parameter data=mylit and voila, multipoints written.

1 Like

Hi All,
Even I have a similar problem that I am trying to solve. I have a curl command which would fetch some metrics as a JSON response. I am storing the JSON response into a variable. Example like the below:
{
“component”: {
“key”: “MY_PROJECT:ElementImpl.java”,
“name”: “ElementImpl.java”,
“qualifier”: “FIL”,
“language”: “java”,
“path”: “src/main/java/com/sonarsource/markdown/impl/ElementImpl.java”,
“measures”: [
{
“metric”: “complexity”,
“value”: “12”,
“period”: {
“value”: “2”,
“bestValue”: false
}
},
{
“metric”: “new_violations”,
“period”: {
“value”: “25”,
“bestValue”: false
}
},
{
“metric”: “ncloc”,
“value”: “114”,
“period”: {
“value”: “3”,
“bestValue”: false
}
}
]
},
“metrics”: [
{
“key”: “complexity”,
“name”: “Complexity”,
“description”: “Cyclomatic complexity”,
“domain”: “Complexity”,
“type”: “INT”,
“higherValuesAreBetter”: false,
“qualitative”: false,
“hidden”: false,
“custom”: false
},
{
“key”: “ncloc”,
“name”: “Lines of code”,
“description”: “Non Commenting Lines of Code”,
“domain”: “Size”,
“type”: “INT”,
“higherValuesAreBetter”: false,
“qualitative”: false,
“hidden”: false,
“custom”: false
},
{
“key”: “new_violations”,
“name”: “New issues”,
“description”: “New Issues”,
“domain”: “Issues”,
“type”: “INT”,
“higherValuesAreBetter”: false,
“qualitative”: true,
“hidden”: false,
“custom”: false
}
],
“period”: {
“mode”: “previous_version”,
“date”: “2016-01-11T10:49:50+0100”,
“parameter”: “1.0-SNAPSHOT”
}
}

However, I need only the metric and the value. Example: complexity=12, new_violations=25 etc etc. This will help me push the data into influxDB using curl is my understanding. Could you please guide and correct me if I am wrong.