【问题标题】:parse nested json from elastic search output by python从 python 的弹性搜索输出中解析嵌套的 json
【发布时间】:2021-08-27 18:04:22
【问题描述】:

使用 Elasticsearch 包,我能够获得此输出。

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'}}]

我正在尝试解析“消息”部分,以便在“创建”之后提出一个来自 {..} 的平面表(例如“Id”、“category”...作为列。)

有人能解释一下吗?我尝试了 res['hits']['hits']['message'] 或 res['hits']['hits'][0] 只是开始,但到目前为止没有运气。

对不起,如果这是一个非常新手的问题,但经过 3 天的努力,我已经放弃了.. -(

【问题讨论】:

    标签: python json parsing elasticsearch


    【解决方案1】:

    您的输出是 JSON 的数组/列表。 你可以尝试使用 for 循环。:

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

    或

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

    【讨论】:

    • 哦,原来是 res['hits']['hits'][0]['message']!!我不知道我也可以做 item.get()。我学到了很多。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2012-07-15
    • 2015-07-30
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 2021-09-01
    • 2015-09-24
    • 2018-09-12
    相关资源
    最近更新 更多