Query JavaScript objects with JSONPath expressions using a robust and safe JSONPath engine for Node.js.
npx @tessl/cli install tessl/npm-jsonpath@1.1.0JSONPath is a robust and safe JavaScript library for querying JavaScript objects using JSONPath expressions. It provides a comprehensive API for extracting data, finding paths, manipulating values, and parsing JSONPath expressions in both Node.js and browser environments.
npm install jsonpathconst jp = require('jsonpath');All functionality is available on the main import:
const jp = require('jsonpath');
// Use as: jp.query(), jp.paths(), jp.nodes(), etc.Browser (using bundled file):
<script src="jsonpath.min.js"></script>
<script>
// Access via global jsonpath object
const jp = jsonpath;
</script>const jp = require('jsonpath');
const data = {
"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
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
};
// Query for all authors
const authors = jp.query(data, '$..author');
// Result: ['Nigel Rees', 'Evelyn Waugh']
// Get paths to all books
const bookPaths = jp.paths(data, '$.store.book[*]');
// Result: [['$', 'store', 'book', 0], ['$', 'store', 'book', 1]]
// Find nodes (path + value) for all prices
const priceNodes = jp.nodes(data, '$..price');
// Result: [
// { path: ['$', 'store', 'book', 0, 'price'], value: 8.95 },
// { path: ['$', 'store', 'book', 1, 'price'], value: 12.99 },
// { path: ['$', 'store', 'bicycle', 'price'], value: 19.95 }
// ]JSONPath is built around several key components:
query, paths, nodes) for finding datavalue, parent, apply)parse, stringify)Core querying functionality for finding elements, paths, and nodes in JavaScript objects using JSONPath expressions. Supports the full JSONPath syntax including wildcards, array slicing, filtering, and recursive descent.
/**
* Find elements matching JSONPath expression
* @param {Object} obj - Target object to query
* @param {string} pathExpression - JSONPath expression string
* @param {number} [count] - Optional limit for number of results
* @returns {Array} Array of matching values
*/
function query(obj, pathExpression, count);
/**
* Find paths to elements matching JSONPath expression
* @param {Object} obj - Target object to query
* @param {string} pathExpression - JSONPath expression string
* @param {number} [count] - Optional limit for number of results
* @returns {Array<Array>} Array of path arrays (each path is array of keys)
*/
function paths(obj, pathExpression, count);
/**
* Find elements and their paths matching JSONPath expression
* @param {Object} obj - Target object to query
* @param {string} pathExpression - JSONPath expression string
* @param {number} [count] - Optional limit for number of results
* @returns {Array<Object>} Array of objects with {path: Array, value: any} structure
*/
function nodes(obj, pathExpression, count);Functions for getting, setting, and transforming values at specific JSONPath locations. Includes support for creating intermediate objects and applying transformations to multiple matching elements.
/**
* Get or set value of first matching element
* @param {Object} obj - Target object
* @param {string|Array} pathExpression - JSONPath expression string or normalized path array
* @param {*} [newValue] - New value to set (optional, creates intermediate objects/arrays as needed)
* @returns {*} Current value (if getting) or new value (if setting)
*/
function value(obj, pathExpression, newValue);
/**
* Get parent of first matching element
* @param {Object} obj - Target object
* @param {string} pathExpression - JSONPath expression string
* @returns {*} Parent object of first match
*/
function parent(obj, pathExpression);
/**
* Apply function to all matching elements, modifying them in place
* @param {Object} obj - Target object
* @param {string} pathExpression - JSONPath expression string
* @param {Function} fn - Transformation function (value) => newValue
* @returns {Array<Object>} Array of modified node objects with {path: Array, value: any}
*/
function apply(obj, pathExpression, fn);Utilities for parsing JSONPath expressions into component objects and converting path arrays back to JSONPath strings. Essential for programmatic manipulation of JSONPath expressions.
/**
* Parse JSONPath expression into component objects
* @param {string} pathExpression - JSONPath expression string
* @returns {Array<Object>} Array of path components with {expression: {type, value}, operation?, scope?}
*/
function parse(pathExpression);
/**
* Convert path array or parsed components to JSONPath expression string
* @param {Array|string} path - Flat array of keys, array of parsed path components, or path string
* @returns {string} JSONPath expression string
*/
function stringify(path);/**
* JSONPath node object containing path and value
* @typedef {Object} JSONPathNode
* @property {Array} path - Array of keys representing location in object
* @property {*} value - The matched value at this location
*/
/**
* JSONPath component object from parsing
* @typedef {Object} JSONPathComponent
* @property {Object} expression - Expression object with type and value
* @property {string} expression.type - Expression type ('root', 'identifier', 'numeric_literal', etc.)
* @property {*} expression.value - Expression value
* @property {string} [operation] - Operation type ('member', 'subscript')
* @property {string} [scope] - Scope type ('child', 'descendant')
*/
/**
* Main JSONPath class for creating custom instances
* Available as: require('jsonpath').JSONPath
*/
class JSONPath {
constructor();
query(obj, pathExpression, count);
paths(obj, pathExpression, count);
nodes(obj, pathExpression, count);
value(obj, pathExpression, newValue);
parent(obj, pathExpression);
apply(obj, pathExpression, fn);
parse(pathExpression);
stringify(path);
}
/**
* Internal Handlers class for path component resolution
* Available as: require('jsonpath').Handlers
*/
class Handlers {
constructor();
resolve(component);
register(key, handler);
}
/**
* Internal Parser class for JSONPath expression parsing
* Available as: require('jsonpath').Parser
*/
class Parser {
constructor();
parse(pathExpression);
}
/**
* Usage examples:
* const jp = require('jsonpath');
* const { JSONPath, Handlers, Parser } = jp;
* const customInstance = new JSONPath();
*/