How to write json in specific format in python - python

I want to display JSON in specific format and data will be from database.So how to form in specific format in python.Format of JSON is
{
"data":{"Device Id" : value from db,"Date":Current Time},
"Data":
{
"Temperature":
{
value from db
},
"Consumptions":
{
value from db
},
"Error":
[
{
value from db
}
]
}
}
I have tried this:
dict((query.description[i][0], value) for i, value in enumerate(row)) for row in query.fetchall()
But I'm not getting desired output and i'm unable to pass data to json. So how to pass data to json.
Error Im getting is Dictionay update sequence element 0# has length 1; 2 is required

Did you use json module?
import json
print json.dumps(yourStructure, indent=6)

Related

How to search inside json file with redis?

Here I set a json object inside a key in a redis. Later I want to perform search on the json file stored in the redis. My search key will always be a json string like in the example below and i want to match this inside the stored json file.
Currently here i am doing this by iterating and comparing but instead i want to do it with redis. How can I do it ?
rd = redis.StrictRedis(host="localhost",port=6379, db=0)
if not rd.get("mykey"):
with open(os.path.join(BASE_DIR, "my_file.josn")) as fl:
data = json.load(fl)
rd.set("mykey", json.dumps(data))
else:
key_values = json.loads(rd.get("mykey"))
search_json_key = {
"key":"value",
"key2": {
"key": "val"
}
}
# here i am searching by iterating and comparing instead i want to do it with redis
for i in key_values['all_data']:
if json.dumps(i) == json.dumps(search_json_key):
# return
# mykey format looks like this:
{
"all_data": [
{
"key":"value",
"key2": {
"key": "val"
}
},
{
"key":"value",
"key2": {
"key": "val"
}
},
{
"key":"value",
"key2": {
"key": "val"
}
},
]
}
To do search with Redis and JSON you have two options - you can use the FT CREATE command to create an index that you can then use FT SEARCH over, (while both of these web pages show the CLI syntax you can do
rd.ft().create() / search() in your python script)
OR you can check out the python OM client that will take care of that to some extent for you.
Either way you'll have to do a bit of a rework to fully take advantage of Redis' search capabilities.

Python is returning json response in single quotes not double (Elasticsearch API)

I am using Elasticsearch library in python to query an elasticsearch server like below:
query = {
"query":{
"match_all":{}
}
}
es = Elasticsearch('localhost:9200')
res = es.search(index='myindex', body=query)
with open('response.json', 'w') as out:
out.write(res)
# json.dump(res, out) also gives the same result
the output I save to file has the format
{
'key':'value',
'key2': {
'key3' : 'value2'
}
}
Notice the single quotes here. I know I can do a simple find and replace or use sed to change single quotes to double, however, I want to know why this is happening when with curl from terminal it is not the case.
I would prefer to have the output dumped in a proper json format
Python dictionary object is not necessarily automatically in json format. The above issue can be solved by using json.dumps() on the response like below:
query = {
"query":{
"match_all":{}
}
}
es = Elasticsearch('localhost:9200')
res = es.search(index='myindex', body=query)
with open('response.json', 'w') as out:
out.write(json.dumps(res))
This will turn res into json format like below:
{
"key":"value",
"key2": {
"key3" : "value2"
}
}

How do I iterate over a json object that has a list of dictionaries inside in Python?

How can I iterate over this to get get MerchantRequestID, CheckoutRequestID, ResultCode, ResultDesc, and all the value in the 'item' list for instance value for PhoneNumber.
Am getting this data from a callBackURl after a user makes a payment.
"Body":{
"stkCallback":{
"MerchantRequestID":"19465-780693-1",
"CheckoutRequestID":"ws_CO_27072017154747416",
"ResultCode":0,
"ResultDesc":"The service request is processed successfully.",
"CallbackMetadata":{
"Item":[
{
"Name":"Amount",
"Value":1
},
{
"Name":"MpesaReceiptNumber",
"Value":"LGR7OWQX0R"
},
{
"Name":"Balance"
},
{
"Name":"TransactionDate",
"Value":20170727154800
},
{
"Name":"PhoneNumber",
"Value":254721566839
}
]
}
}
}
}```
I want to get MerchantRequestID, CheckoutRequestID, ResultCode, ResultDesc, and all the value in the list
'item' then store them in the db.
```new_user = MpesaResponses(MerchantRequestID=data[0]['Body']['stkCallback']['MerchantRequestID'],
CheckoutRequestID=data[0]['Body']['stkCallback']['CheckoutRequestID'],
ResultCode=data[0]['Body']['stkCallback']['ResultCode'],
ResultDesc=data[0]['Body']['stkCallback']['ResultDesc'],
Amount=data[0]['Body']['stkCallback']['CallbackMetadata']['Item'][0]
['value'],
MpesaReceiptNumber=data[0]['Body']['stkCallback']['CallbackMetadata']
['Item'][1]['value'],
TransactionDate=data[0]['Body']['stkCallback']['CallbackMetadata']['Item']
[3]['value'],
PhoneNumber=data[0]['Body']['stkCallback']['CallbackMetadata']['Item'][4]
['value'])
db.session.add(new_user)
db.session.commit()```
This is what I had tried.
If the data is just a dictionary starting at "Body" and not a list containing a dictionary, then just remove the first [0] from each variable, then check your spelling on Value as so:
MerchantRequestID=data['Body']['stkCallback']['MerchantRequestID']
CheckoutRequestID=data['Body']['stkCallback']['CheckoutRequestID']
ResultCode=data['Body']['stkCallback']['ResultCode']
ResultDesc=data['Body']['stkCallback']['ResultDesc']
Amount=data['Body']['stkCallback']['CallbackMetadata']['Item'][0]['Value']
MpesaReceiptNumber=data['Body']['stkCallback']['CallbackMetadata']['Item'][1]['Value']
TransactionDate=data['Body']['stkCallback']['CallbackMetadata']['Item'][3]['Value']
PhoneNumber=data['Body']['stkCallback']['CallbackMetadata']['Item'][4]['Value']

compare elements from list in a json file

I have a scenario where I am trying to compare list elements with a json file and if there is a match then return certain values and create a json response.
here are my json data
[
{
"evi": 1223,
"evn": "testapp1",
"conf": {
"c_gr": "tot",
"c_id": "112"
}
},
{
"evi": 6759,
"evn": "testapp2",
"conf": {
"c_gr": "tot",
"c_id": "112"
}
},
{
"evi": 3352,
"evn": "testapp3",
"conf": {
"c_gr": "tot7",
"c_id": "112"
}
}
]
Here is what I have tried so far :
response=requests.post('https://testaapp.com', headers=headers, data=data)
resp_json=response.json()
if response.status_code == 200:
print ('working fine...!!')
else:
print ('notworking !!')
metadata=resp_json['data']
m_list=[1123, 123445, 61887, 3352, 35811488976]
final_lst_data1 = []
final_lst_data2 = []
for key in m_list:
temp1= key
for keya in metadata:
if temp1 in metadata[keya]['evi']:
final_lst_data1.append(metadata['evn']) #return the names
final_lst_data2.append(metadata['evn'], metadata['conf']) #return evn and conf values after checking and then dump it to json from list in same json format if not possible then convert to json from list, not sure how to do this here.
But this doesnt works as it gives me below error
if key in metadata[keya]['evi']:
TypeError: list indices must be integers or slices, not dict
You are using a dictionary as an index. When you say "for keya in metadata:", keya is a dictionary that refers to the list of dictionaries inside 'metadata'. So, you don't need to use metadata[keya] to access each element, you can just use 'keya'.
for keya in metadata:
if temp1 == keya['evi']:
final_lst_data1.append(keya['evn'])
final_lst_data2.append([keya['evn'], keya['conf']])

Getting TypeError while spliting Json data?

I have A json data like this:
json_data = '{"data":"[{"Date":"3/17/2017","Steam Total":60},{"Date":"3/18/2017","Steam Total":15},{"Date":"3/19/2017","Steam Total":1578},{"Date":"3/20/2017","Steam Total":1604}]", "data_details": "{"data_key":"Steam Total", "given_high":"1500", "given_low":"1000", "running_info": []}"}'
json_input_data = json_data["data"]
json_input_additional_info = json_data["data_details"]
I am getting an error:
TypeError: string indices must be integers, not str
I think there is an error in the json data. Can someone Help me on this?
In you code has some issues.
The code: json_input_data = json_data["data"], the variable json_data is not a Json Object, is a String Object and you try get a string position by string key, for get a Json object from string json use json api: json
You Json string isn't valid, this is a valid version:
{"data":[{"Date":"3/17/2017","Steam Total":60},{"Date":"3/18/2017","Steam Total":15},{"Date":"3/19/2017","Steam Total":1578},{"Date":"3/20/2017","Steam Total":1604}], "data_details": {"data_key":"Steam Total", "given_high":"1500", "given_low":"1000", "running_info": []}}
Now, your code works fine.
Try parsing your json_data to JSON format (with JSON.parse(json_data)). Currently it's type is string - which is exactly what your error says.
As Pongpira Upra pointed out, your json is not well formed and should be something like this.
{
"data":[
{
"Date":"3/17/2017",
"Steam Total":60
},
{
"Date":"3/18/2017",
"Steam Total":15
},
{
"Date":"3/19/2017",
"Steam Total":1578
},
{
"Date":"3/20/2017",
"Steam Total":1604
}
],
"data_details":{
"data_key":"Steam Total",
"given_high":"1500",
"given_low":"1000",
"running_info":[]
}
}
In order to retrieve information you should write
json_data[0]["Date"]
This would print "3/17/2017"
You declare a string called json_data and, well, then it acts like a string. That is what the exception tells you. Like others here tried to say - you do also have an error in your data, but the exception you supplied is due to accessing the string as if it was a dictionary. You need to add a missing call to e.g. json.loads(...).
You were right. Your JSON is indeed wrong.
Can you try using this json?
{
"data":[
{
"Date":"3/17/2017",
"Steam Total":60
},
{
"Date":"3/18/2017",
"Steam Total":15
},
{
"Date":"3/19/2017",
"Steam Total":1578
},
{
"Date":"3/20/2017",
"Steam Total":1604
}
],
"data_details":{
"data_key":"Steam Total",
"given_high":"1500",
"given_low":"1000",
"running_info":[]
}
}

Categories

Resources