Reputation: 2327
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
Upvotes: 0
Views: 758
Reputation: 2327
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"
}
}
Upvotes: 1