简体   繁体   English

从弹性搜索 output by python 解析嵌套的 json

[英]parse nested json from elastic search output by python

With Elasticsearch package I was able to get this output.使用 Elasticsearch package 我能够得到这个 output。

res = es.search(index="*:*-logs-ndb-*", body=body, size=1) 
output=res['hits']['hits']

output

[{'_type': '_doc',
  '_source': {'Id': '10032',
   'log_format': 'plain',
   'consumer_time': '2021-06-09T08:55:58.115',
   'type': 'application',
   'message': 'Created {\n  "Id": "10032",\n  "category": {\n    "type": "Scheduled"\n  },\n  "api": false,\n  "settings": {},\n  "LogicInfo": {\n    "isReq": false\n  }\n}',
   'index_pattern': 'main'}}]

I'm trying to parse "message" part so I can come up with a flat table that came from {.. } after 'Created (eg "Id", "category"... as column.)我正在尝试解析“消息”部分,以便在“创建”之后提出一个来自 {..} 的平面表(例如“Id”、“category”...作为列。)

Can anyone shed light on this?任何人都可以阐明这一点吗? I tried res['hits']['hits']['message'] or res['hits']['hits'][0] just to start with but no luck so far.我尝试了 res['hits']['hits']['message'] 或 res['hits']['hits'][0] 只是开始,但到目前为止还没有运气。

Sorry if this is very newbie question but I've given up after 3 days' struggle.. -(对不起,如果这是一个非常新手的问题,但经过 3 天的努力我已经放弃了.. -(

Your output is an array/list of JSON.您的 output 是 JSON 的数组/列表。 you can try using for loop.:您可以尝试使用 for 循环。:

for item in output:
    message = item.get('message')

or或者

res['hits']['hits'][0]['message']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM