npm.io
3.0.1 • Published 6d ago

json-p3

Licence
MIT
Version
3.0.1
Deps
1
Size
469 kB
Vulns
0
Weekly
0
Stars
31

JSON P3

JSONPath, JSON Patch and JSON Pointer for JavaScript.
We follow RFC 9535 and test against the JSONPath Compliance Test Suite.

LICENSE Tests NPM npm type definitions


Example

import { jsonpath } from "json-p3";

const expr = "$.users[?@.status == 'pending'].name";

const data = {
  users: [
    { id: "usr_101", name: "Alice", status: "pending" },
    { id: "usr_102", name: "Bob", status: "active" },
    { id: "usr_103", name: "Charlie", status: "pending" },
  ],
};

const nodes = jsonpath.find(expr, data);

console.log(nodes.map((n) => n.value)); // [ "Alice", "Charlie" ]
console.log(nodes.map((n) => n.location)); // [ [ "users", 0, "name" ], [ "users", 2, "name" ] ]
console.log(nodes.map((n) => n.normalizedPath())); // [ "$['users'][0]['name']", "$['users'][2]['name']" ]
console.log(nodes.map((n) => n.shorthandPath())); // [ "$.users[0].name", "$.users[2].name" ]
console.log(nodes.map((n) => n.pointer.toString())); // [ "/users/0/name", "/users/2/name" ]

Contributing

Please see Contributing to JSON P3

License

json-p3 is distributed under the terms of the MIT license.

Keywords