Python jsonpath Filter Expression
Background:
I have the following example data structure in JSON:
{'sensor' : [ {'assertions_enabled': 'ucr+', 'deassertions_enabled': 'ucr+', 'entity_id': '7.0', 'lower_critical': 'na', 'lower_non_critical': 'na', 'lower_non_recoverable': 'na', 'reading_type': 'analog', 'sensor_id': 'SR5680 TEMP (0x5d)', 'sensor_reading': {'confidence_interval': '0.500', 'units': 'degrees C', 'value': '42'}, 'sensor_type': 'Temperature', 'status': 'ok', 'upper_critical': '59.000', 'upper_non_critical': 'na', 'upper_non_recoverable': 'na'} ]}
The sensor list will actually contain many of these dicts containing sensor info.
Problem:
I'm trying to query the list using jsonpath to return me a subset of sensor dicts that have sensor_type=='Temperature' but I'm getting 'False' returned (no match). Here's my jsonpath expression:
results = jsonpath.jsonpath(ipmi_node, "$.sensor[?(@.['sensor_type']=='Temperature')]")
When I remove the filter expression and just use "$.sensor.*" I get a list of all sensors, so I'm sure the problem is in the filter expression.
I've scanned multiple sites/posts for examples and I can't seem to find anything specific to Python (Javascript and PHP seem to be more prominent). Could anyone offer some guidance please?
Answers
The following expression does what you need (notice how the attribute is specified):
jsonpath.jsonpath(impi_node, "$.sensor[?(@.sensor_type=='Temperature')]")