Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hi there Monks
I have to extract some values from a JSON data. I have a sample here where I cant get to one of the values in one of the blocks. I would like to have this as a result:
But I can not get to the value:
Instead, I am getting just this:
Any help on that?
Thanks for looking!
I have to extract some values from a JSON data. I have a sample here where I cant get to one of the values in one of the blocks. I would like to have this as a result:
Yes: Mary Lou - 11111 | 1 Yes: Mary Lou - 22222 | 2 Yes: John De - 33333 | 1 Yes: Smith Doe - 4444 | 1 No sec value, just the name: Joseph D.
But I can not get to the value:
Yes: Mary Lou - 11111 | 1
Instead, I am getting just this:
Yes: Mary Lou - 22222 | 2 Yes: John De - 33333 | 1 Yes: Smith Doe - 4444 | 1 No sec value, just the name: Joseph D.
Any help on that?
#!/usr/bin/perl use strict; use warnings; use JSON::XS; use JSON qw( ); use Data::Dumper; my $json = '{ "required": [ { "docs": [ { "sec": "11111", "number": "1" } ], "docs": [ { "sec": "22222", "number": "2" } ], "name": "Mary Lou" }, { "docs": [ { "sec": "33333", "number": "1" } ], "name": "John De" }, { "docs": [ { "sec": "4444", "number": "1" } ], "name": "Smith Doe" }, { "name": "Joseph D." } ] }'; my $json_data = JSON->new; my $data = $json_data->decode($json); for my $dta ( @{ $data->{ required } || [] } ){ if( scalar @{ $dta->{ docs } || [] } ){ for my $doc_data ( @{ $dta->{ docs } || [] } ){ if( $doc_data->{'sec'} ) { print "\n Yes: $dta->{'name'} - $doc_data->{'sec'} | $doc_da +ta->{'number'} \n"; } } }else { warn "\n No sec value, just the name: $dta->{'name'} \n\n"; } }
Thanks for looking!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Parsing nested JSON
by hippo (Bishop) on Oct 04, 2021 at 21:26 UTC | |
Re: Parsing nested JSON
by AnomalousMonk (Archbishop) on Oct 05, 2021 at 00:28 UTC | |
by BillKSmith (Monsignor) on Oct 05, 2021 at 17:09 UTC | |
by LanX (Saint) on Oct 06, 2021 at 09:20 UTC | |
Re: Parsing nested JSON (duplicate keys)
by LanX (Saint) on Oct 04, 2021 at 21:56 UTC |
Back to
Seekers of Perl Wisdom