0

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

#!/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. 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. Also I cannot use anything else from Python - this script is just example.

Is this possible with jmespath? It is currently my best option due to limitations of project.

halfer
  • 18,701
  • 13
  • 79
  • 158
HAXiAL
  • 1
  • 3
  • The question is do You have to use JMES for this? What You are trying to do would be much easier done using MongoDB or other no-sql document based engine. – Tetlanesh Jan 04 '18 at 23:59

1 Answers1

1

Removing fields with Jmespath is not possible currently. There is a pending feature request:

Ability to set and delete based on a jmespath #121 https://github.com/jmespath/jmespath.py/issues/121

I am using jq to do that:

jq 'del(.foo)'
Input   {"foo": 42, "bar": 9001, "baz": 42}
Output  {"bar": 9001, "baz": 42}
mromer
  • 1,816
  • 1
  • 12
  • 16