简体   繁体   English

用jmespath过滤掉Json

[英]Filtering out Json with jmespath

I have this simple Python script, it is supposed to be part of something bigger I just cannot figure out how to work with jmespath 我有这个简单的Python脚本,应该是更大的一部分,我只是无法弄清楚如何使用jmespath

#!/usr/bin/env python

import jmespath

if __name__ == '__main__':
# input json
text = \
{
    'topology': [
        {
            'node': [
                {
                    'topology-stats:session-state': {
                        'synchronized': True,
                        'local-pref': {
                            'session-id': 0,
                            'deadtimer': 120,
                            'ip-address': '10.30.170.187',
                            'keepalive': 30
                        },
                        'messages': {
                            'stateful-stats:sent-upd-msg-count': 1,
                            'last-sent-msg-timestamp': 1513334157,
                            'stateful-stats:last-received-rpt-msg-timestamp': 1513334157,
                            'unknown-msg-received': 0,
                            'stateful-stats:received-rpt-msg-count': 3,
                            'reply-time': {
                                'max-time': 77,
                                'average-time': 77,
                                'min-time': 77
                            },
                            'stateful-stats:sent-init-msg-count': 0,
                            'sent-msg-count': 1,
                            'received-msg-count': 3
                        },
                        'session-duration': '0:00:00:12'
                    },
                    'node-id': '10.30.170.117'
                }
            ],
            'topology-id': 'asdf-topology'
        }
    ]
}

exp = jmespath.compile('''topology[*].node[?'topology-stats:session-state' != 'session-duration'][]''')
result = exp.search(text)

print result

What I want is to basically remove lines with keys which have unpredictable values ( in perfect world I would switch the value for something generic ) - like: last-sent-msg-timestamp, session-duration, stateful-stats:last-received-rpt-msg-timestamp. 我想要的是基本上删除具有无法预测的值的键的行(在理想情况下,我会切换通用值的值),例如:last-sent-msg-timestamp,session-duration,stateful-stats:last-received- RPT-MSG-时间戳。 And perfectly I would want to keep everything else, although I can live with loosing topology and node tags. 尽管我可以忍受松散的拓扑结构和节点标签,但我还是想保留所有其他东西。

The thing is I can use only one jmespath.search and I can do only one jmespath expression. 问题是我只能使用一个jmespath.search,而我只能做一个jmespath表达式。 Also I cannot use anything else from Python - this script is just example. 另外,我不能使用Python的其他任何功能-此脚本只是示例。

Is this possible with jmespath? jmespath有可能吗? It is currently my best option due to limitations of project. 由于项目的限制,目前是我最好的选择。

Removing fields with Jmespath is not possible currently. 当前无法使用Jmespath删除字段。 There is a pending feature request: 有一个待处理的功能请求:

Ability to set and delete based on a jmespath #121 https://github.com/jmespath/jmespath.py/issues/121 可以基于jmespath#121进行设置和删除的功能https://github.com/jmespath/jmespath.py/issues/121

I am using jq to do that: 我正在使用jq来做到这一点:

jq 'del(.foo)'
Input   {"foo": 42, "bar": 9001, "baz": 42}
Output  {"bar": 9001, "baz": 42}

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

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