Download Download from Github


Download Download from Github

J-Path is a tiny, performant library for querying and transforming JSON documents with XPath expressions. It is interfaced via the main function, jpath(), (see API) which returns an array of matched items from the input data.

Example 🔗

Example 1: J-Path - pull some items out of the input data via J-Path:

let data = {foo: {bar: ['woop', 2, 3]}} jpath(data, 'foo/bar|//bar/item[1]'); //[Array, "woop"] jpath(data, '//item[text() = 2 or text() = 3]'); //[2, 3]

See also - blog: Transforming and querying JSON with J-Path.


  • jpath(input, path) - the main function, to return data from input (a JSON string or JavaScript object) targeted by path (an XPath expression). Returns an array of items - which may be of any data type depending on what was targeted.
  • jpath.obj_to_xml(obj) - convert a JavaScript object, obj, to an XML string. This utility is provided merely as a convenience, since J-Path internally converts input data first to XML (see Concepts & methodology). As discussed, array items will be given the node name item.

Internal conversion 🔗

In order that the object can be traversed via XPath, J-Path internally converts it to an XML document, and it is this that you ultimately query.

For this reason it's important to be aware of how J-Path handles this conversion.

You can always see the XML structure J-Path will derive by first calling the helper method jpath.obj_to_xml() on your object - see API.

Tag names are derived from object properties - but for arrays, tag names will always be item. So for example, J-Path would convert this:

{foo: [1, 3, 5]}

...into this:

<foo> <item>1</item> <item>3</item> <item>5</item> </foo>

Thus you could retrieve the third value of the array with a query such as:


If your object is an array, it will be wrapped in the root node root, i.e.:

[{foo: 'bar'}, {bar: 'foo'}]


<root> <item> <foo>bar</foo> </item> <item> <foo>bar</foo> </item> </root>

However this root node is for container purposes only - it does not form part of your query. Thus, with this data, to retrieve the value of the foo node in the first item, we would do:




Download Download from Github

Did I help you? Feel free to be amazing and buy me a coffee on Ko-fi!