3 JSONPath 功能

    概述

    JSONPath由用点分隔的段组成。 段可以是一个简单的词,如 JSON 值名称、,也可以是括在方括号 [ ] 中的更复杂的构造。 括号段前的分隔点是可选的,可以省略。 例如:

    参考: .

    支持的段

    格式描述
    <name>按名称匹配对象属性。
    匹配所有对象属性。
    [‘<name>’]按名称匹配对象属性。
    [‘<name>’, ‘<name>’, …]通过任何列出的名称匹配对象属性。
    [<index>]按索引匹配数组元素。
    [<number>, <number>, …]通过任何列出的索引匹配数组元素。
    []匹配所有对象属性或数组元素。
    [<start>:<end>]按定义的范围匹配数组元素:
    <start> - 要匹配的第一个索引(包括)。 如果未指定,则匹配从头开始的所有数组元素。 如果为负数,则指定从数组末尾开始的偏移量。
    <end> - 要匹配的最后一个索引(不包括)。 如果未指定,则匹配所有数组元素到最后。 如果为负数,则指定从数组末尾开始的偏移量。
    [?(<表达式>)]通过应用过滤表达式匹配对象/数组元素。

    要查找忽略其根节点的匹配段(单独的段),它必须以 ‘..’ 为前缀,例如 $..name$..['name'] 返回所有 ‘name’ 属性的值。

    可以通过在 JSONPath 中添加 ~ 后缀来提取匹配的元素名称。 它返回匹配对象的名称或匹配数组项的字符串格式的索引。 输出格式遵循与其他 JSONPath 查询相同的规则 - 确定路径结果按“原样”返回,不确定路径结果以数组形式返回。 但是,提取与明确路径匹配的元素的名称并没有多大意义——它是已知的。

    过滤表达式

    支持的操作:

    支持的操作:

    操作类型描述结果
    -二进制减法数字
    二进制加法数字
    /二进制除法数字
    *二进制乘法数字
    ==二进制等于布尔值(1 或 0)
    !=二进制不等于布尔值(1 或 0)
    <二进制小于布尔值(1 或 0)
    <=二进制小于或等于布尔值(1 或 0)
    >二进制大于布尔值(1 或 0)
    >=二进制大于或等于布尔值(1 或 0)
    =~二进制匹配正则表达式布尔值(1 或 0)
    !一元非布尔值布尔值(1 或 0)
    ||二进制布尔值或布尔值(1 或 0。
    &&二进制布尔值和布尔值(1 或 0)

    函数

    函数可以用在 JSONPath 的末尾。 如果前面的函数返回后面函数接受的值,则可以链接多个函数。

    支持的功能:

    不兼容的输入会导致函数产生错误。

    输出值

    JSONPaths 可以分为确定路径和不确定路径。 确定路径只能返回 null 或单个匹配项。 不定路径可以返回多个匹配项,基本上是带有分离的、多个名称/索引列表、数组切片或表达式段的 JSONPath。 但是,当使用函数时,JSONPath 变得明确,因为函数总是输出单个值。

    确定路径返回它所引用的对象/数组/值,而不定路径返回匹配的对象/数组/值的数组。

    空格

    空格(空格、制表符)可以在括号符号段和表达式中自由使用,例如,$[ 'a' ][ 0 ][ ?( $.b == 'c' ) ][ : -1 ].first( ).

    字符串

    示例

    输入数据
    JSONPath数据类型结果注释
    $.filters.pricedefinite10
    $.filters.categorydefinitefiction
    $.filters[‘no filters’]definiteno “filters”
    $.filtersdefinite{
    “price”: 10,
    “category”: “fiction”,
    “no filters”: “no \”filters\””
    }
    $.books[1].titledefiniteSword of Honour
    $.books[-1].authordefiniteJ. R. R. Tolkien
    $.books.length()definite4
    $.tags[:]indefinite[“a”, “b”, “c”, “d”, “e” ]
    indefinite[“c”, “d”, “e” ]
    $.tags[:3]indefinite[“a”, “b”, “c”]
    $.tags[1:4]indefinite[“b”, “c”, “d”]
    $.tags[-2:]indefinite[“d”, “e”]
    $.tags[:-3]indefinite[“a”, “b”]
    $.tags[:-3].length()definite2
    $.books[0, 2].titleindefinite[“Sayings of the Century”, “Moby Dick”]
    $.books[1][‘author’, “title”]indefinite[“Evelyn Waugh”, “Sword of Honour”]
    $..idindefinite[1, 2, 3, 4]
    $.services..priceindefinite[5, 154.99, 46, 24.5, 99.49]
    $.books[?(@.id == 4 - 0.4 5)].titleindefinite[“Sword of Honour”]此查询表明可以在查询中使用算术运算。当然这个查询可以简化为 $.books[?(@.id == 2)].title
    $.books[?(@.id == 2 || @.id == 4)].titleindefinite[“Sword of Honour”, “The Lord of the Rings”]
    $.books[?(!(@.id == 2))].titleindefinite[“Sayings of the Century”, “Moby Dick”, “The Lord of the Rings”]
    $.books[?(@.id != 2)].titleindefinite[“Sayings of the Century”, “Moby Dick”, “The Lord of the Rings”]
    $.books[?(@.title =~ “ of “)].titleindefinite[“Sayings of the Century”, “Sword of Honour”, “The Lord of the Rings”]
    $.books[?(@.price > 12.99)].titleindefinite[“The Lord of the Rings”]
    $.books[?(@.author > “Herman Melville”)].titleindefinite[“Sayings of the Century”, “The Lord of the Rings”]
    $.books[?(@.price > $.filters.price)].titleindefinite[“Sword of Honour”, “The Lord of the Rings”]
    $.books[?(@.category == $.filters.category)].titleindefinite[“Sword of Honour”,”Moby Dick”,”The Lord of the Rings”]
    $..[?(@.id)]indefinite[
    {
    “category”: “reference”,
    “author”: “Nigel Rees”,
    “title”: “Sayings of the Century”,
    “price”: 8.95,
    “id”: 1
    },
    {
    “category”: “fiction”,
    “author”: “Evelyn Waugh”,
    “title”: “Sword of Honour”,
    “price”: 12.99,
    “id”: 2
    },
    {
    “category”: “fiction”,
    “author”: “Herman Melville”,
    “title”: “Moby Dick”,
    “isbn”: “0-553-21311-3”,
    “price”: 8.99,
    “id”: 3
    },
    {
    “category”: “fiction”,
    “author”: “J. R. R. Tolkien”,
    “title”: “The Lord of the Rings”,
    “isbn”: “0-395-19395-8”,
    “price”: 22.99,
    “id”: 4
    }
    ]
    $.services..[?(@.price > 50)].descriptionindefinite‘[“Printing and assembling book in A5 format”, “Rebinding torn book”]
    $..id.length()definite4
    $.books[?(@.id == 2)].title.first()definiteSword of Honour
    $..tags.first().length()definite5$..tags 是不定路径,所以它返回一个匹配元素的数组 - [[“a”, “b”, “c”, “d”, “e” ]], first() 返回第一个元素 - [“a”, “b”, “c”, “d”, “e” ] 和最后ength() 计算它的长度 - 5.
    $.books[].price.min()definite8.95
    $..price.max()definite154.99
    $.books[?(@.category == “fiction”)].price.avg()definite14.99
    $.books[?(@.category == $.filters.xyz)].titleindefinite不匹配的查询对于明确和不明确的路径返回 NULL。
    $.services[?(@.active==”true”)].servicegroupindefinite[1000,1001]在布尔值比较中必须使用文本常量。
    indefinite[1002]在布尔值比较中必须使用文本常量。
    $.services[?(@.servicegroup==”1002”)]~.first()definite恢复