use warnings; use strict; use JSON; my $original = '{"OWNER":"KeyProjects","age1":null,"ht1":null}'; my $desired = '{"OWNER":"KeyProjects","scheduled":{"age1":null,"ht1":null}}'; my $data = decode_json($original); for my $key ( keys %{ $data } ) { print "Found a key: $key", $/; if ( $key =~ /\A (?: age\d+ | ht\d+ ) \z/x ) { print " -> It matches our regular expression\n"; print " -> Deleting it from data structure\n"; my $value = delete $data->{$key}; print " -> Putting it back under sub-structure with key 'scheduled'\n"; $data->{scheduled}{$key} = $value; } else { print " -> It's fine where it is, no action taken\n"; } } print encode_json( $data ), $/;