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?


In reply to How to access an array in a JSON file which is the root element (using JSON::Path) by Bloehdian

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.