The following code prints all the "leaves" of a complex data structure but you can add selection criteria operating on the keys as required.
use strict; use warnings; use JSON; my %dispatch = ('' => sub { print "$_[0]\n"; }, 'HASH' => sub { for my $key (keys %{$_[0]}) { search_and_replace_in_hash( $_[0]->{$key} +); } }, 'ARRAY' => sub { search_and_replace_in_hash( $_ ) for @{$_[0]}; + }, 'default' => sub { my ( $element, $op ) = @_; print STDERR ref($element).": unknown type of +reference\n" }, ); sub search_and_replace_in_hash { &{ $dispatch{ ref($_[0]) } // $dispatch{'default'} }(@_); } my $data = <<EOD; { "1": {"subject1": "value", "subject2": [{"subject3": "value", "subject +4": "value"}], "subject5": "value", "subject6": "value", "subject7": +"value"}, "2": {"subject1": "value", "subject2": [{"subject3": "value", "subject +4": "value"}], "subject5": "value", "subject6": "value", "subject7": +"value"}, "3": {"subject1": "value", "subject2": [{"subject3": "value", "subject +4": "value"}], "subject5": "value", "subject6": "value", "subject7": +"value"} } EOD $data = JSON->new()->decode( $data ); search_and_replace_in_hash( $data );
In reply to Re: JSON Structure Question
by hdb
in thread JSON Structure Question
by omegaweaponZ
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |