多个不同路径的 JSONPath 并集
JSONPath union of multiple different paths
我正在尝试构建一个单个 JSONPath 查询,它将测试是否存在两个或多个路径。
让我们考虑以下示例文档:
{
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"address" : {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
},
"phoneNumbers": [
{
"type" : "iPhone",
"number": "0123-4567-8888"
},
{
"type" : "home",
"number": "0123-4567-8910"
}
]
}
到目前为止我发现:
$..[firstName,lastName,type]
并从整个文档中获取所有这些元素。
但是,我需要检查两个不同的路径,例如:
$.firstName
$.address.city
这可以通过单个 JSONPath 查询来完成吗?我不能写这样的东西:
$.[firstName,address.city]
使用 XML 和 XPath 我可以写:
/person/firstname | /person/address/city
并获得所有匹配 XML 元素的并集。
我可以用 JSONPath 做同样的事情吗?
我认为最接近原始 JSONPath 的是使用递归下降和联合,即
$..['firstName','city']
Goessner 实施将 return
[
"John",
"Nara"
]
kubernetes JSONPath 支持联合运算符的扩展,允许
[‘metadata.name’, ‘status.capacity’]
其中 name
和 capacity
是 metadata
和 status
的成员。
JSONMatch是JSONPath的一个变体,最初基于Kubernetes JSONPath解析器,支持完全独立路径的联合,例如
[employee[5].name, company.name, wageTiers[compensation > 10000]]
JSONMatch 在 go and I believe javascript 中也可用。
少数 JSONPath 实现支持 bracket notation with two literals separated by dot and bracket notation with two literals separated by dot without quotes。
jsoncons C++ library(自 0.161.0 起)支持完全独立路径的 JSONPath 并集与符号
$..['firstName',@.address.city]
这个答案不是问题的 100% 答案,因为它假设 nodejs/javascript。因为它描述了我们如何克服 JSONPath 的限制,所以我希望它可以帮助其他人。
我们有类似的要求。然而,我们需要寻找数百条不同的路径并对它们进行处理,性能是一个实际问题。然后我们将 JSONPath 替换为 object-scan。它有点受限,但是当你需要做数据处理时它有一些优势。
以下是解决问题的方法(请注意,您可以将编译和 运行 部分分开以使后续执行的性能更高):
// const objectScan = require('object-scan');
const data = { firstName: 'John', lastName: 'doe', age: 26, address: { streetAddress: 'naist street', city: 'Nara', postalCode: '630-0192' }, phoneNumbers: [{ type: 'iPhone', number: '0123-4567-8888' }, { type: 'home', number: '0123-4567-8910' }] };
const r = objectScan(['firstName', 'address.city'], { rtn: 'count' })(data);
console.log(r);
// => 2
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan@13.8.0"></script>
免责声明:我是object-scan
的作者
我正在尝试构建一个单个 JSONPath 查询,它将测试是否存在两个或多个路径。
让我们考虑以下示例文档:
{
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"address" : {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
},
"phoneNumbers": [
{
"type" : "iPhone",
"number": "0123-4567-8888"
},
{
"type" : "home",
"number": "0123-4567-8910"
}
]
}
到目前为止我发现:
$..[firstName,lastName,type]
并从整个文档中获取所有这些元素。
但是,我需要检查两个不同的路径,例如:
$.firstName
$.address.city
这可以通过单个 JSONPath 查询来完成吗?我不能写这样的东西:
$.[firstName,address.city]
使用 XML 和 XPath 我可以写:
/person/firstname | /person/address/city
并获得所有匹配 XML 元素的并集。
我可以用 JSONPath 做同样的事情吗?
我认为最接近原始 JSONPath 的是使用递归下降和联合,即
$..['firstName','city']
Goessner 实施将 return
[
"John",
"Nara"
]
kubernetes JSONPath 支持联合运算符的扩展,允许
[‘metadata.name’, ‘status.capacity’]
其中 name
和 capacity
是 metadata
和 status
的成员。
JSONMatch是JSONPath的一个变体,最初基于Kubernetes JSONPath解析器,支持完全独立路径的联合,例如
[employee[5].name, company.name, wageTiers[compensation > 10000]]
JSONMatch 在 go and I believe javascript 中也可用。
少数 JSONPath 实现支持 bracket notation with two literals separated by dot and bracket notation with two literals separated by dot without quotes。
jsoncons C++ library(自 0.161.0 起)支持完全独立路径的 JSONPath 并集与符号
$..['firstName',@.address.city]
这个答案不是问题的 100% 答案,因为它假设 nodejs/javascript。因为它描述了我们如何克服 JSONPath 的限制,所以我希望它可以帮助其他人。
我们有类似的要求。然而,我们需要寻找数百条不同的路径并对它们进行处理,性能是一个实际问题。然后我们将 JSONPath 替换为 object-scan。它有点受限,但是当你需要做数据处理时它有一些优势。
以下是解决问题的方法(请注意,您可以将编译和 运行 部分分开以使后续执行的性能更高):
// const objectScan = require('object-scan');
const data = { firstName: 'John', lastName: 'doe', age: 26, address: { streetAddress: 'naist street', city: 'Nara', postalCode: '630-0192' }, phoneNumbers: [{ type: 'iPhone', number: '0123-4567-8888' }, { type: 'home', number: '0123-4567-8910' }] };
const r = objectScan(['firstName', 'address.city'], { rtn: 'count' })(data);
console.log(r);
// => 2
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan@13.8.0"></script>
免责声明:我是object-scan
的作者