使用jsonpath获取父节点 [英] Using jsonpath to get parent node

查看:163
本文介绍了使用jsonpath获取父节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用节点JSONPath,如何从子节点值中获取父节点名称

Using node JSONPath, how can I get the parent node name from child node value

{
  "store": {
    "book": [
      {
        "id":"1",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "id":"2",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      }
    ]
  }
}

我使用此表达式基于值标识子节点,我想使用此子节点查找父节点

I use this expression to identify the child node based on value, I want to use this child node to find the parent node

$.[?(@.id =="1")]

推荐答案

您没有指定JSON路径的实现,但是通过加特林(Scala)和JayWay(Java),您都可以使用嵌套过滤器在返回时按子项进行过滤父母,祖父母或其他任何人.这是一个示例:

You do not specify which implementation of JSON Path, but with both Gatling (Scala) and JayWay (Java) you can use nested filters to filter by children while returning the parent, grandparent or whatever. Here is a sample:

使用此JSON:

{
  "a": {
    "b": {
        "c": {
            "d": {
                "e": "foo"
            }
        },
        "something": "bar"
    }
  }
}

这条路径:

$.a.b[?(@.c[?(@.d[?(@.e == "foo")])])].something

返回:

[ "bar" ]

我能够通过使用表达式来获取b,以到达较低的节点作为过滤器.

I am able to retrieve b by using expressions to get to lower nodes as the filter.

一些其他实现在这些表达式上出错.

Some other implementations error out on these expressions.

这篇关于使用jsonpath获取父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆