Operator | Description |
@ | The current node. |
* | Wildcard, which can match any node name or index value. |
.<name> | Use "." to match child nodes. |
[<number> (, <number>)] | Retrieve one or more elements in an array using []. |
[start:end] | Array slicing |
[?(<expression>)] | Filter data with a Boolean 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);});}