dchester / jsonpath

Query and manipulate JavaScript objects with JSONPath expressions. Robust JSONPath engine for Node.js.
MIT License
1.33k stars 215 forks source link

jp.stringify fails on top level array data #132

Open cthorner opened 5 years ago

cthorner commented 5 years ago

For array data

[
{ "asdf": 1}, 
{ "asdf": 2}
]

I am not able to get stringify to work for a give path

to reproduce:

jp.stringify([1, "asdf"])

throws: Uncaught Error: couldn't understand path 1,name at JSONPath._normalize (jsonpath.js:5100)

movitto commented 4 years ago

@cthorner stringify takes an input representative of JSONPath components, either as an array of strings or an array of parsed path expressions, and converts them into a string path:

jp.stringify(path)

Returns a path expression in string form, given a path. The supplied path may either be a flat array of keys, as returned by jp.nodes for example, or may alternatively be a fully parsed path expression in the form of an array of path components as returned by jp.parse.

var pathExpression = jp.stringify(['$', 'store', 'book', 0, 'author']);
// "$.store.book[0].author

Parsed path expressions must consist of an array of objects each containing 'operation', 'scope', and 'expression' members. The internal parser is responsible for converting a given string into an array in this format. See JSONPath#_normalize (private) method for how arrays of individual path component strings are converted into arrays of these objects.

Your input to stringify isn't compliant with any of these valid input types, hence you are getting an error