[英]How to return the json in a required format
Json is below Json在下方
result = {
"took" : 21,
"timed_out" : False,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "data",
"_type" : "_doc",
"_id" : "qwcs",
"_score" : 1.0,
"_source" : {
"id" : "10",
"name" : "Country ",
"description" : "This product contains all currency details",
"Owner" : {
"id" : "11",
"Name" : "David",
"Email" : "nons@utc.com",
"role" : "Analyst"
},
"Area" : [
"Data Management"
],
"Type" : [
"API",
"TXT"
],
"Level" : [
"A"
]
}
}
]
}
}
I wrote the python code to extract the data from elastic through api hit and above is the result我编写了 python 代码,通过 api 命中从弹性中提取数据,上面是结果
sample api: http://utc.com/search/Owner.id?=11样本 api: http://utc.com/search/Owner.id?=11
Back-end query will generate {'query': {'match': {'Owner.id': '11'}}}
后端查询会生成
{'query': {'match': {'Owner.id': '11'}}}
But i need only small details the expected out is below但我只需要一些小细节,预期结果如下
"Owner": { "id": "11", "Name": "David", "Email": "nons@utc.com", "role": "Analyst" }
If you're saying you want to return only the Owner
s in the hits
list with an id
matching your query, you can use a list comprehension:如果您说您只想返回
hits
列表中具有与您的查询匹配的id
的Owner
,则可以使用列表理解:
query = {'query': {'match': {'Owner.id': '11'}}}
owners = [hit['_source']['Owner'] for hit in result['hits']['hits']
if hit['_source']['Owner']['id'] == query['query']['match']['Owner.id']]
print(owners)
Output: Output:
[{'id': '11', 'Name': 'David', 'Email': 'nons@utc.com', 'role': 'Analyst'}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.