JSON Pathfinder is a javascript library dedicated to finding the line number for a given JSONPath query in a json string
Use the package manager npm to install json-pathfinder.
npm -i @jeanbono/json-pathfinderimport { jsonPathfinder } from '@jeanbono/json-pathfinder';
const jsonString = '{\n\
"foo": "bar",\n\
"baz": [{ \n\
"hello": "world" \n\
}]\n\
}';
// parse the json string
const pathfinder = jsonPathfinder(jsonString);
// returns line number 4
console.log(pathfinder('$.baz[0].hello'));Queries follow RFC 9535 (JSONPath),
restricted to singular queries — the root identifier $ followed by
member-name and array-index segments. Wildcards, slices, filters, and
descendant selectors aren't supported, since they can match more than one
location and this library always resolves to exactly one line.
Both forms below are accepted and resolve to the same result — bracket
notation is required for member names that aren't valid identifiers
(containing a literal ., [, whitespace, ...):
pathfinder('$.baz[0].hello');
pathfinder("$['baz'][0]['hello']");A syntactically invalid query throws; a valid query with no matching
location returns -1.
Note: versions before 2.0.0 used a bare
a.b[0].csyntax without a leading$. Prefix existing queries with$.(or switch to bracket notation) to upgrade.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.