jsonpath icon indicating copy to clipboard operation
jsonpath copied to clipboard

Path of '$' breaks for some methods

Open delucis opened this issue 7 years ago • 2 comments

I’m not sure if this is intentional, but while a path of '$' — i.e. the root object — works fine with jp.query(), jp.paths(), jp.nodes(), jp.parse(), and jp.value() when not setting a new value, it causes an error for other methods. That makes sense for jp.parent() (although returning null or undefined might be friendlier than throwing an error), but it would be nice if jp.apply() and setting a new value with jp.value() worked too.

var jp = require('jsonpath')
let o = { foo: 'things' }
jp.value(o, '$')          // returns { foo: 'things' }
let newO = { bar: 'baz' }
jp.value(o, '$', newO)    // throws error “couldn’t understand path” in jp._normalize()

You can test this example on RunKit: https://runkit.com/embed/f3g7kw86i0z6

I realise that using jsonpath with a path of '$' is redundant, but wondered whether this behaviour was expected or not.

delucis avatar Aug 28 '17 13:08 delucis

I ran into this bug today and was surprised. I agree that it feels redundant but $ is a valid JSON path so, although odd, I expected it to work.

I'll look into creating a PR this week.

cameronhunter avatar Jun 01 '19 07:06 cameronhunter

Still running into issues with this.

beckyconning avatar Feb 07 '22 18:02 beckyconning