# JSONPath

> 在 Proxyman 中使用 JSONPath 查询 JSON 请求和响应正文。了解语法、过滤运算符，以及可直接复制的路径示例。

## 1. 这是什么？ {#1-whats-it}

JSONPath 是一种可以快速过滤 JSON 数据的查询语言。在 Proxyman 中，你可以在 JSON 标签页、Treeview 标签页以及[自定义列](https://proxyman.com/docs/zh-cn/basic-features/custom-header-column)中使用它。

![Proxyman 的 JSON 标签页，使用 JSON Path 表达式过滤响应正文](https://proxyman.com/docs/assets/Screenshot%202026-08-25%20at%2014.04.30.jpg)

## 2. 使用方法 {#2-how-to-use}

1. 在 Request 或 Response 面板中，点击 **+** 按钮
2. 选择 **JSON**，添加 JSON 标签页
3. 过滤栏会显示在 JSON 正文上方
4. 选择 **JSON Path** 或 **jq** 过滤模式

## 3. 语法 {#3-syntax}

| 运算符 | 说明 |
| --- | --- |
| `$` | 要查询的根元素。所有路径表达式都以它开头。 |
| `@` | 过滤谓词当前正在处理的节点。 |
| `*` | 通配符。可用于任何需要名称或数字的位置。 |
| `..` | 深度扫描。可用于任何需要名称的位置。 |
| `.<name>` | 点号表示法的子元素 |
| `['<name>' (, '<name>')]` | 方括号表示法的一个或多个子元素 |
| `[<number> (, <number>)]` | 数组索引 |
| `[start:end]` | 数组切片运算符 |
| `[?(<expression>)]` | 过滤表达式。表达式的结果必须为布尔值。 |

### 过滤运算符 {#filter-operators}

过滤器是用于过滤数组的逻辑表达式。典型的过滤器为 `[?(@.age > 18)]`，其中 `@` 表示当前正在处理的元素。

| 运算符 | 说明 |
| --- | --- |
| == | 左侧等于右侧（注意 1 与 '1' 不相等） |
| != | 左侧不等于右侧 |
| < | 左侧小于右侧 |
| <= | 左侧小于或等于右侧 |
| > | 左侧大于右侧 |
| >= | 左侧大于或等于右侧 |
| =\~ | 左侧匹配正则表达式 \[?(@.name =\~ /foo.\*?/i)] |
| in | 左侧存在于右侧中 \[?(@.size in \['S', 'M'])] |
| nin | 左侧不存在于右侧中 |
| subsetof | 左侧是右侧的子集 \[?(@.sizes subsetof \['S', 'M', 'L'])] |
| anyof | 左侧与右侧有交集 \[?(@.sizes anyof \['M', 'L'])] |
| noneof | 左侧与右侧没有交集 \[?(@.sizes noneof \['M', 'L'])] |
| size | 左侧（数组或字符串）的长度应与右侧相等 |
| empty | 左侧（数组或字符串）应为空 |

### 路径示例 {#path-examples}

以下面的 JSON 数据为例。

```javascript
{
    "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
}
```

| JsonPath | 结果 |
| --- | --- |
| $.store.book\[\*].author | 所有图书的作者 |
| $..author | 所有作者 |
| $.store.\* | 所有内容，包括图书和自行车 |
| $.store..price | 所有商品的价格 |
| $..book\[2] | 第三本书 |
| $..book\[-2] | 倒数第二本书 |
| $..book\[0,1] | 前两本书 |
| $..book\[:2] | 从索引 0（含）到索引 2（不含）的所有图书 |
| $..book\[1:2] | 从索引 1（含）到索引 2（不含）的所有图书 |
| $..book\[-2:] | 最后两本书 |
| $..book\[2:] | 从索引 2（含）到末尾的所有图书 |
| $..book\[?(@.isbn)] | 所有带有 ISBN 编号的图书 |
| $.store.book\[?(@.price < 10)] | 店内所有价格低于 10 的图书 |
| $..book\[?(@.price <= $\['expensive'])] | 店内所有不算“昂贵”（价格不高于 expensive 的值）的图书 |
| $..book\[?(@.author =\~ /.\*REES/i)] | 所有匹配正则表达式的图书（忽略大小写） |
| $..\* | 获取所有内容 |
| $..book.length() | 图书的数量 |

## 参考资料 {#reference}

* [GitHub 上的 Jayway JsonPath](https://github.com/json-path/JsonPath)
