Using Raku (formerly known as Perl_6)
~$ raku -MJSON::Tiny -e 'my %hash = from-json($_) given lines;
my @a = %hash.values.map({ $_.values if $_{"list1"} });
.say for @a.sort.join(" ");' file
OR:
~$ raku -MJSON::Tiny -e 'my %hash = from-json($_) given lines;
for %hash.values.sort() { print .values.sort ~ " " if $_{"list1"} };
put "";' file
Raku is a programming language in the Perl-family that provides high-level support for Unicode. Like Perl, Raku has associative arrays (hashes and/or maps) built-in. The above code is admittedly rather verbose (first example), but you should be able to get the flavor of the language from both examples above:
- Raku's community-supported
JSON::Tiny is called at the command line,
- All
lines are given as one data element to the from-json function, which decodes the input and stores it in %hash,
- First Example: Using a
map, the values of the hash are searched through for "list1" keys. If (if) found, these are stored in the @a array. Then the @a array is printed.
- Second Example: the
%hash is iterated through using for, searched through for "list1" keys, and if found the associated values are printed (with at end-of-line). A final put call adds a newline.
Sample Input (includes bogus "list2" elements)
{
"key1": {
"list1": [
"val1",
"val2",
"val3"
]
},
"key2": {
"list1": [
"val4",
"val5"
]
},
"key3": {
"list1": [
"val6"
]
},
"key4": {
"list2": [
"val7"
]
}
}
Sample Output:
val1 val2 val3 val4 val5 val6
Finally, in any programming solution it is often instructive to look at intermediate data-structures. So here's what the %hash looks like after decoding JSON input:
~$ raku -MJSON::Tiny -e 'my %hash = from-json($_) given lines; .say for %hash.sort;' file
key1 => {list1 => [val1 val2 val3]}
key2 => {list1 => [val4 val5]}
key3 => {list1 => [val6]}
key4 => {list2 => [val7]}
https://raku.land/cpan:MORITZ/JSON::Tiny
https://docs.raku.org/language/hashmap
https://raku.org