user1188611
user1188611

Reputation: 955

Modifying data structure that will be encoded using JSON in perl

I have a JSON whcih i am trying to modify using perl code.

{
    "Person":{

       "personalData": {
          "workList": {
              "file":{ 
                  "fileName": "/usr/temp/ABC.txt" }
                },
            }
         }
       }   
    }  

I need to convert the above JSON into something like this:

{
    "Person":{

       "personalData": {
          "workList": {
              "directoryList":{
                   "directory":[
                       "file":{ 
                           "fileName": "/usr/temp/ABC.txt" }
                          }
                     ]
                }
            }
         }
       }   
    }

Can someone give some example of doing this in perl.

Upvotes: 1

Views: 105

Answers (1)

mob
mob

Reputation: 118605

$data->{Person}{personalData}{workList}{directoryList}{directory} =
    [ delete $data->{Person}{personalData}{workList}{file} ];

Or more concisely,

$tmp = $data->{Person}{personalData}{workList};
$tmp->{directoryList}{directory} = [ delete $tmp->{file} ];

For an explanation of how this works see: How to replace a Perl hash key?

Upvotes: 2

Related Questions