在Python中,使用jsonpath-rw获取特定属性的值(json / dict)

2024-06-05 08:31:54 发布

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

这是我的json:

{
   'test': [
        { "id": "1", "description": "Test 1" },
        { "id": "2", "description": "Test 2" }
    ]
}

我试图得到id的值,其中描述是“测试1”。

我在JsonPath页面上找到了以下示例:

$..book[?(@.price<10)]

尝试分析以下jsonxpath表达式时:

parse('$..test[?(@.description="Test 1")].id')

我得到以下错误:

jsonpath_rw.lexer.JsonPathLexerError: Error on line 1, col 7: Unexpected character: ?

我做错什么了?或者,有没有更好的方法来做到这一点?


Tags: testidjson示例parse表达式错误页面