全部文档
文档中心数据流功能参考JSON PATH介绍

JSON PATH介绍

类似于XPath在XML文档中的定位,JSONPath表达式通常是用来路径检索或设置JSON的。其表达式可以接受’dot–notation’和’bracket–notation’格式,例如[‘store’][‘book’][0][‘title’] JsonPath是一种简单的方法来提取给定JSON文档的部分内容。 JsonPath有许多编程语言,如Javascript,Python和PHP,Java。 JsonPath提供的json解析非常强大,它提供了类似正则表达式的语法,基本上可以满足所有你想要获得的json内容。 github上有它的应用:https://github.com/json-path/JsonPath JSONPath在线解析器:http://www.atoolbox.net/Tool.php?Id=792

符号

描述

$

查询的根节点对象,用于表示一个json数据,可以是数组或对象

@

过滤器断言(filter predicate)处理的当前节点对象,类似于java中的this字段

*

通配符,可以表示一个名字或数字

..

可以理解为递归搜索,可以遍历JSON中的所有层级的键值对

.

表示一个子节点

[’

’ (, ‘
’)]

表示一个或多个子节点

[

(,
)]

表示一个或多个数组下标

[start:end]

数组片段,区间为[start,end),不包含end

[?(

)]

过滤器表达式,表达式结果必须是boolean

名称

描述

输出数据类型

min()

获取数值类型数组的最小值

Double

max()

获取数值类型数组的最大值

Double

avg()

获取数值类型数组的平均值

Double

stddev()

获取数值类型数组的标准差

Double

length

获取数值类型数组的长度

Int

过滤器是用于过滤数组的逻辑表达式,一个通常的表达式形如:[?(@.age > 18)],可以通过逻辑表达式&&或||组合多个过滤器表达式,例如[?(@.price < 10 && @.category == ‘fiction’)]。 字符串必须用单引号或双引号包围,例如[?(@.color == ‘blue’)] or [?(@.color == ‘blue’)]。

操作符

描述

==

等于,但数字1不等于字符’1’

!=

不等于

<

小于

<=

小于等于

>

大于

>=

大于等于

=~

判断是否符合正则表达式,例如[?(@.name =~ /foo.*?/i)]

in

属于符号,例如[?(@.size in [‘S’, ‘M’])]

nin

排除符号(not in)

size

长度比较符号,判断字符串或数组的长度是否一样

empty

空值符号

Copy
{
  "store": {
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  },
  "expensive": 10
}

JSON PATH表达式

含义

输出结果

..author

查询store.book中的所有author的值,遍历递归查询整个JSON对象中的author的值

[‘Nigel Rees’,’Evelyn Waugh’,’Herman Melville’,’J. R. R. Tolkien’]

$.store.*

查询store下级节点里的所有值

[ [ { ‘category’ : ‘reference’, ‘author’ : ‘Nigel Rees’, ‘title’ : ‘Sayings of the Century’, ‘price’ : 8.95}, { ‘category’ : ‘fiction’, ‘author’ : ‘Evelyn Waugh’, ‘title’ : ‘Sword of Honour’, ‘price’ : 12.99 }, { ‘category’ : ‘fiction’,’author’ : ‘Herman Melville’, ‘title’ : ‘Moby Dick’, ‘isbn’ : ‘0-553-21311-3’, ‘price’ : 8.99 }, { ‘category’ : ‘fiction’, ‘author’ : ‘J. R. R. Tolkien’,’title’ : ‘The Lord of the Rings’, ‘isbn’ : ‘0-395-19395-8’,’price’ : 22.99 } ], {‘color’ : ‘red’,’price’ : 19.95}]

$.store..price

在store所有的后续节点中查询price的值

[8.95,12.99,8.99,22.99,19.95]

..book[:2]

查询前两个book的值

[{‘category’ : ‘reference’,’author’ : ‘Nigel Rees’,’title’ : ‘Sayings of the Century’,’price’ : 8.95},{‘category’ : ‘fiction’,’author’ : ‘Evelyn Waugh’,’title’ : ‘Sword of Honour’,’price’ : 12.99}]

$..book[-2:]

查询最后两个book的值

[{‘category’ : ‘fiction’,’author’ : ‘Herman Melville’,’title’ : ‘Moby Dick’’isbn’ : ‘0-553-21311-3’,’price’ : 8.99},{‘category’ : ‘fiction’,’author’ : ‘J. R. R. Tolkien’,’title’ : ‘The Lord of the Rings’,’isbn’ : ‘0-395-19395-8’,’price’ : 22.99}]

$..book[2:]

查询book第2个值以后的所有值

[{‘category’ : ‘fiction’,’author’ : ‘Herman Melville’,’title’ : ‘Moby Dick’,’isbn’ : ‘0-553-21311-3’,’price’ : 8.99},{‘category’ : ‘fiction’,’author’ : ‘J. R. R. Tolkien’,’title’ : ‘The Lord of the Rings’,’isbn’ : ‘0-395-19395-8’,’price’ : 22.99}]

$..book[?(@.isbn)]

查询具有isbn键的所有book的值

[ { “category”: “fiction”, “author”: “Herman Melville”,”title”: “Moby Dick”,”isbn”: “0-553-21311-3”, “price”: 8.99 }, { “category”: “fiction”, “author”: “J. R. R. Tolkien”,”title”: “The Lord of the Rings”,”isbn”: “0-395-19395-8”, “price”: 22.99}]

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

查询所有price<10的book的值

[ { “category”: “fiction”, “author”: “Evelyn Waugh”,”title”: “Sword of Honour”, “price”: 12.99 },{ “category”: “fiction”, “author”: “J. R. R. Tolkien”, “title”: “The Lord of the Rings”, “isbn”: “0-395-19395-8”, “price”: 22.99 }]

[‘expensive’])]

查询所有price<expensive的book的值

[ { “category”: “fiction”,”author”: “Evelyn Waugh”, “title”: “Sword of Honour”, “price”: 12.99},{ “category”: “fiction”, “author”: “J. R. R. Tolkien”, “title”: “The Lord of the Rings”, “isbn”: “0-395-19395-8”,”price”: 22.99}]

$..book[?(@.author =~ /.*Rees/i)]

查询所有符合正则表达式的book的值

[{‘category’ : ‘reference’,’author’ : ‘Nigel Rees’,’title’ : ‘Sayings of the Century’,’price’ : 8.95}]

$..*

返回当前查询的整个JSON对象

返回当前查询的整个JSON对象

$..book.length()

查询所有book的值组成的数组的长度

[4]

回到顶部

咨询热线

400-821-9199

我们使用 ChatGPT,基于文档中心的内容以及对话上下文回答您的问题。

ctrl+Enter to send