Python jsonpath Filter Expression


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.


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?


The following expression does what you need (notice how the attribute is specified):

jsonpath.jsonpath(impi_node, "$.sensor[?(@.sensor_type=='Temperature')]")

Need Your Help

Reload points on a Map in using the Google Maps API v3

javascript jquery json google-maps google-maps-api-3

I'm trying to reload points on a map based on when someone clicks a button. “BasePage” class ideas


What cool functionality and methods do you add to your BasePage : System.Web.UI.Page classes?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.