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

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

  • Comment on Re: How to access an array in a JSON file which is the root element (using JSON::Path)
  • Select or Download Code