Just another Perl shrine | |
PerlMonks |
How to access an array in a JSON file which is the root element (using JSON::Path)by Bloehdian (Beadle) |
on Apr 01, 2018 at 21:55 UTC ( [id://1212113]=perlquestion: print w/replies, xml ) | Need Help?? |
Bloehdian has asked for the wisdom of the Perl Monks concerning the following question: I intend to print the elements of an array stored in a JSON file using JSON::Path with the following Perl one-liner: perl -e 'use JSON::Path; $json = "[ \"string1\", \"string2\" ]"; $json_path = JSON::Path->new( "\$" ); foreach $elem ( @{$json_path->values( $json )} ) { print "##$elem##" . "\n" }'But instead of the strings contained in the array surrounded by two pairs of hashes I just get the warning: Use of uninitialized value in string eq at /usr/local/share/perl/5.20.2/JSON/Path/Evaluator.pm line 108.I would expect that the JSONPath "$" would address the root element in the JSON file (which is an array) and, hence, $json_path->values( $json ) would point to a list/ an array with the elements being the strings "string1" and "string2". Dereferencing $json_path->values( $json ) would deliver the array whose elements could be printed out then. But obviously I am wrong. How can I properly address an array in a JSON file which is the root element?
Back to
Seekers of Perl Wisdom
|
|