产品动态


运算符 | 描述 |
@ | 当前节点 |
* | 通配符,可匹配任意节点名或索引值 |
.<name> | 用 . 匹配下级节点 |
[<number> (, <number>)] | 用 [] 检索数组中的一个或多个元素 |
[start:end] | 数组切片 |
[?(<expression>)] | 用布尔表达式筛选数据 |
import jsonpath from 'pts/jsonpath';export default function () {const json = JSON.stringify({"name": {"first": "Tom", "last": "Anderson"},"age": 37,"children": ["Sara", "Alex", "Jack"],"fav.movie": "Deer Hunter","friends": [{"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},{"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},{"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}]});console.log(jsonpath.get(json, 'name.last')); // Andersonconsole.log(jsonpath.get(json, 'age')); // 37console.log(jsonpath.get(json, 'children')); // Sara,Alex,Jackconsole.log(jsonpath.get(json, 'children[*]')); // Sara,Alex,Jackconsole.log(jsonpath.get(json, 'children.[0]')); // Saraconsole.log(jsonpath.get(json, 'children[1:2]')); // Alex,Jackconsole.log(jsonpath.get(json, 'children[1, 2]')); // Alex,Jackconsole.log(jsonpath.get(json, 'friends[:].first')); // Dale,Roger,Janeconsole.log(jsonpath.get(json, 'friends[1].last')); // Craigconsole.log(jsonpath.get(json, 'friends[?(@.age > 45)].last')); // Craig,Murphyconsole.log(jsonpath.get(json, 'friends[?(@.first =~ /D.*e/)].last')); // Murphy};


import { sleep, check } from "pts";import http from "pts/http";export default function main() {let response;response = http.get("http://mockhttpbin.pts.svc.cluster.local/get?name=hello");check("body matches /h[a-z]*o/", () => {const expr = new RegExp("h[a-z]*o");return expr.test(response.body);});}
文档反馈