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.

update

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 ;)


In reply to Translate or morph hash keys to different names by rmcgowan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.