rmcgowan has asked for the wisdom of the Perl Monks concerning the following question:
I'm accessing a REST API interface to an Oracle database. The API is written in Java. The retrieved data is JSON encoded. I cannot make any changes on the server side, and have to live with whatever is sent.
There are a number of field and table names that have been changed on the server side for various reasons. For example 'abstract' is 'incidentAbstract', and 'cross_reference' is 'crossReference', in the JSON encoding.
I have a number of scripts that have been developed over a decade or more, that use DBI and DBD::Oracle to access data, using the database names. I now need to port these to using the REST API and I'm trying to do this with minimal changes to the scripts themselves. To do this, I need a way to restore the original field or table names for the modified ones in the JSON decoded data. For example:
Before After { { incidentAbstract => 'string' abstract => 'string' } }
I found a module on CPAN, Hash::KeyMorpher, that almost does the job, but not quite. It only changes the style of the name, but keeps the name, as in KeyFile to key_file or vice versa.
I can write my own, but would like to be sure someone hasn't already done this, or that there isn't a "better way" to handle it.
Thanks.
Many thanks for the pointers to Hash::Map and Data::Visitor::Callback, etc. I had actually looked at Data::Dumper as a potential candidate but decided there might be a "simpler", or at least different, module I could use.
It also turns out the JSON modules have a method, filter_json_object, that could also work:
#!/usr/bin/perl -w use strict; use Data::Dumper; # Testing JSON filter_json_object method, to convert 'field names'. use JSON::XS; my $sub = sub { my $jsRef = shift; my %map = (one => 'uno', two => 'dos', three => 'tres' +); my $hRef; foreach my $k (keys %$jsRef) { $$hRef{$map{$k}} = $$jsRef{$k}; } return $hRef; }; my $hRef = {one => 1, two => 2, three => 3}; print Dumper ($hRef), "\n\n"; #my $json = JSON:XS->new->filter_json_object->encode ($hRef); my $json = JSON::XS->new->allow_nonref->filter_json_object ($sub); my $jsonStr = $json->encode ($hRef); #print "$jsonStr\n"; my $newHRef = $json->decode ($jsonStr); print Dumper ($newHRef); --- $VAR1 = { 'three' => 3, 'one' => 1, 'two' => 2 }; $VAR1 = { 'uno' => 1, 'dos' => 2, 'tres' => 3 };
I'll be able to do what's needed with one or another of the suggestions, I've no doubt ;)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Translate or morph hash keys to different names
by LanX (Saint) on Nov 06, 2013 at 20:32 UTC | |
by rmcgowan (Sexton) on Nov 06, 2013 at 20:53 UTC | |
|
Re: Translate or morph hash keys to different names
by kschwab (Vicar) on Nov 06, 2013 at 20:56 UTC | |
|
Re: Translate or morph hash keys to different names (Data::Walk, Data::Rmap, Data::Visitor::Callback)
by Anonymous Monk on Nov 06, 2013 at 21:03 UTC | |
|
Re: Translate or morph hash keys to different names
by locked_user sundialsvc4 (Abbot) on Nov 07, 2013 at 14:43 UTC |