Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: How to properly check a JSONPath for existance?

by kroach (Pilgrim)
on Apr 02, 2018 at 16:24 UTC ( #1212179=note: print w/replies, xml ) Need Help??

in reply to How to properly check a JSONPath for existance?

The documentation says the values() method returns the number of matches but it doesn't seem to be the case since in your example it returns undef for both a non-existent element and a null one. Since you check for defined(), they both return false.

What you can do is call values() in list context and check for the number of matches. The non-existent entry should have none and null should have a single undef entry.

my @values = $json_path[$i]->values($json); if (@values > 0) { print("exists\n"); } else { print("does not exist\n") }

Replies are listed 'Best First'.
Re^2: How to properly check a JSONPath for existance?
by Bloehdian (Beadle) on Apr 02, 2018 at 17:26 UTC

    Works! Thanks a lot!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1212179]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2020-10-23 12:09 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (238 votes). Check out past polls.