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.