jsonpath
jsonpath copied to clipboard
Path of '$' breaks for some methods
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.
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.
Still running into issues with this.