如何使用JSONPath查找不含Type属性的Children子级键?
用JSONPath查找Children下不含Type属性的对象键
原始JSON结构
{ "Type": "page", "Label": "Test", "Children": { "crud": { "Type": "crud", "Datasource": "crud_data", "Children": { "level1": {}, "group": { "Type": "group", "level2": {}, "Children": { "level3a": {}, "level3b": {}, "level3c": {} } } } } } }
需求说明
需要编写JSONPath表达式,找出所有位于Children节点下、自身不包含Type属性的对象的键。
期望结果
[ "level1", "level3a", "level3b", "level3c" ]
注:crud和group因包含Type属性被排除;即使对象含有Type以外的其他属性,只要没有Type就需要被匹配。
我初步的思路是这样的:$..Children.*[?(a filter here saying no Type)]~,但不确定过滤条件怎么写。
解决方案
最终JSONPath表达式
$..Children.*[?(!@.Type)]~
表达式拆解
$..Children:递归遍历所有名为Children的节点.*:匹配每个Children节点下的所有子对象[?(!@.Type)]:过滤逻辑,@指代当前遍历到的子对象,!@.Type表示当前对象不存在Type属性~:指定返回匹配对象的键名(而非对象本身)
这个表达式可以精准筛选出符合要求的键,完全匹配期望的结果。
内容的提问来源于stack exchange,提问作者Cristof
相关产品推荐
相关产品推荐

