If you want the entire content of the array you would probably use this path:

$.[0:]

values() will not return an array reference, in that case, but rather a list of the array's contents. So your foreach loop would look like this:

foreach $elem ($json_path->values($json)) { ... }

In other words, no need to dereference to get a list. You're getting a list in the first place.

Also, instead of all those backslash escapes, consider using q{['string1', 'string2']}. This is much easier to read than "[\"string1\", \"string2\"]".

Update: So, the entire one-liner would probably be better written like this:

perl -MJSON::Path -Mstrict -wE 'my $json = q{["string1","string2"]}; m +y $json_path = JSON::Path->new(q{$.[0:]}); foreach my $elem ($json_pa +th->values($json)) {say "##$elem$$"}'

Here we're using the -M command line switch to 'use' the JSON::Path module. We can use that switch as many times as we need, so we could also have said, "-Mwarnings". I also used the -E switch, which loads more modern Perl features. In this case I used it to gain a say keyword to use in place of print. Doing so allowed me to not have to append a newline. A small but convenient savings in one-liners. And I used the -w switch to enable warnings (I could have achieved a similar effect by using -Mwarnings. Finally, despite this being a throwaway one-liner, I used lexical variables, mostly out of habit, but also partially because it's a good way to catch typos in variable names, which can be a factor even in short one-liners.

We're not doing this to count keystrokes, but it's worth pointing out that adding strictures, warnings, say, lexical my variables, and a thicker quoting construct (q{...} vs "...") improved the quality of the one-liner and at the same time didn't increase its size. We used fewer keystrokes while cutting down on the difficult portions like escaping quote characters, as well as the tedious parts like appending newlines.


Dave


In reply to Re: How to access an array in a JSON file which is the root element (using JSON::Path) by davido
in thread 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.