php - How to iterate a multidimensional json_decoded object in Perl to create a new json object? -
i'm trying recreate php process in perl , not having luck ( don't know perl ).
this php code:
$json = '{"0":{"name":"action","value":"none"},"1":{"name":"additionaltraining","value":"modulesrevisited"},"2":{"name":"additionaltrainingcomments","value":""},"3":{"name":"additionaltraining","value":"modulereporting"},"4":{"name":"additionaltrainingcomments","value":""},"5":{"name":"additionaltrainingcomments","value":""},"6":{"name":"anothervalue","value":""},"7":{"name":"1359436206","value":""},"8":{"name":"1359436207","value":""},"48":{"name":"actionid","value":""},"49":{"name":"multiselect","value":"second"},"50":{"name":"multiselect","value":"third"},"51":{"name":"radio","value":"1"},"52":{"name":"svgs","value":{"0":{"id":"drawing"},"1":{"id":"drawing"}}}}'; $decoded = json_decode($json,true); $clean = array(); foreach($decoded $obj => $array){ if(array_key_exists($array['name'], $clean)) { if(!is_array($clean[$array['name']])) { $value = $clean[$array['name']]; $clean[$array['name']] = array($value,$array['value']); } else { array_push($clean[$array['name']], $array['value']); } } else { $clean[$array['name']]=$array['value']; } } echo json_encode($clean);
update: i've tried in perl , walls keep hitting:
use json; use data::dumper; use warnings; use strict; @records = decode_json($json_text); foreach $element (@records) { print "$element\n"; }
i hashref. try changing first line to:
my @records = @{ decode_json($json_text) }; #dereference function returns arrayref, not list
then "not array reference" when trying loop through.
then try , no results or errors:
my $records = decode_json($json_text); $i = 0; @records; foreach $entry (@{$records[0]}) { %listhash = %{$entry}; $key; $value; $i++; while(($key, $value) = each(%listhash)) { $key; $value; print "${key}_$i, $value\n"; } }
then try , combine bits of of them output i'm after , closest come is:
my %records = %{ decode_json($json_text) }; while ( ($key, $value) = each %records ) { $records; #print "key: $key, value: $records{$key}\n"; while ( ($key, $value) = each %{$value} ) { print "key: $key, value: $value\n"; } }
but gives me output so:
key: name, value: anothervalue key: value, value: third key: name, value: multiselect key: value, value: modulereporting
whereas need output
key: anothervalue, value: third key: multiselect, value: modulereporting
where going wrong?
how can recreate same result in perl? appreciated.
well, aren't going wrong; json format being stupid possible. json hash of keys (which sorted, numeric strings) map level of hashes, have keys name
, value
:
{ "0": {"name": "somekey", "value": "somevalue"}, "1": {"name": "anotherkey", "value": "anothervalue"} }
(this data should encoded [{"somekey": "somevalue"}, {"anotherkey": "anothervalue"}]
.)
so we'll loop through numerically sorted keys outer hash:
for $index (sort { $a <=> $b } keys %records) { ... }
inside loop, not loop through keys/values of hash values. instead, pick values of keys name
, value
:
my $record_value = $records{$index}; ($key, $value) = @$record_value{qw/name value/}; # "hash slice"
and print them:
say "key: $key, value: $value";
Comments
Post a Comment