Python jsonpath筛选器表达式

2024-06-06 09:56:26 发布

您现在位置:Python中文网/ 问答频道 /正文

背景:

下面是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'}
]}

传感器列表实际上包含许多包含传感器信息的指令。

问题:

我试图使用jsonpath查询列表,以返回具有sensor_type=='Temperature'但返回'False'的传感器指令子集(不匹配)。下面是我的jsonpath表达式:

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

当我移除过滤器表达式并使用"$.sensor.*"时,我会得到所有传感器的列表,因此我确信问题出在过滤器表达式中。

我已经扫描了多个站点/帖子以获取示例,但似乎找不到任何特定于Python的内容(Javascript和PHP似乎更突出)。有人能提供一些指导吗?


Tags: 示例列表表达式typeenabled传感器sensorupper