小编Ted*_*ann的帖子

在 jsonpath-ng python 中使用 OR 运算符 (|)

我有一个 json 结构 (client_json),如下所示:

client_json = { "data": [ { "attributes": { "creators": [ { "name": "This is a person", "nameType": "Personal", "givenName": "the", "familyName": "person" }, { "name": "This is an organization", "nameType": "Organizational", "givenName": "the", "familyName": "organization" } ] } } ] }
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 jsonpath-ng 1.4.3 ( https://github.com/h2non/jsonpath-ng ) 来检索创建者名称。

from jsonpath_ng import jsonpath
from jsonpath_ng.ext import parse
Run Code Online (Sandbox Code Playgroud)

这两个表达式给出了预期的结果:

[match.value['name'] for match in parse("data[*].attributes.creators[?(@.nameType='Personal')]").find(client_json)]
[match.value['name'] for match in parse("data[*].attributes.creators[?(@.nameType='Organizational')]").find(client_json)]
Run Code Online (Sandbox Code Playgroud)

我试图将它们与 or 运算符结合起来,所有这些表达式都会产生解析错误:

parse("data[*].attributes.creators[?(@.nameType == 'Organizational' | @.nameType == …
Run Code Online (Sandbox Code Playgroud)

python json operators jsonpath

5
推荐指数
1
解决办法
2175
查看次数

标签 统计

json ×1

jsonpath ×1

operators ×1

python ×1