title: Remap JSON object to other JSON structure
body: <p>I'm trying to remap the following JSON structure that is formatted by categories and then per category can contain multiple locations. A location contains a lon/lat and an area code:
  </p>
  
  <pre class="lang-js prettyprint-override"><code>{
    "cat1":[
      {"location":{
        "latitude":51.38,
        "longitude":4.34,
        "code":"0873"}
      },
      {"location":{
        "latitude":52.65,
        "longitude":6.74,
        "code":"0109"}
      },
      {"location":{
        "latitude":51.48,
        "longitude":4.33,
        "code":"0748"}
      },
      {"location":{
        "latitude":51.48,
        "longitude":4.33,
        "code":"0109"}
      }
    ],
    "cat2":[
      {"location":{
        "latitude":52.33,
        "longitude":4.32,
        "code":"0873"}
      },
      {"location":{
        "latitude":52.65,
        "longitude":6.74,
        "code":"0109"}
      },
      {"location":{
        "latitude":51.48,
        "longitude":4.33,
        "code":"0728"}
      }
    ],
    "cat3":[
      {"location":{
        "latitude":52.33,
        "longitude":4.32,
        "code":"0873"}
      },
      {"location":{
        "latitude":52.65,
        "longitude":6.74,
        "code":"0109"}
      },
      {"location":{
        "latitude":51.48,
        "longitude":4.33,
        "code":"0758"}
      }
    ]
  }
  </code></pre>
  
  <p>Into the following structure, that is primarily focussed on the areacode, then has the categories with the actual locations in them.</p>
  
  <pre class="lang-js prettyprint-override"><code>{
    "code":[
      {"0873":[
        {"cat1":[
          {"location":{"latitude":51.38,"longitude":4.34}}
        ]},
        {"cat2":[
          {"location":{"latitude":52.33,"longitude":4.32}}
        ]},
        {"cat3":[
          {"location":{"latitude":52.33,"longitude":4.32}}
        ]}
      ]},
  
      {"0109":[
        {"cat1":[
          {"location":{"latitude":52.65,"longitude":6.74}},
          {"location":{"latitude":51.48,"longitude":4.33}}
        ]},
        {"cat2":[
          {"location":{"latitude":52.65,"longitude":6.74}}
        ]},
        {"cat3":[
          {"location":{"latitude":52.65,"longitude":6.74}}
        ]}
  
      ]},
      {"0748":[
        {"cat1":[
          {"location":{"latitude":51.48,"longitude":4.33}}
        ]}
      ]},
  
      {"0728":[
        {"cat2":[
          {"location":{"latitude":51.48,"longitude":4.33}}
        ]}
      ]},
      {"0758":[
        {"cat3":[
          {"location":{"latitude":51.48,"longitude":4.33}}
        ]}
      ]}
  
    ]
  }
  </code></pre>
  
  <p>I try to do this in Javascript/Node and was looking at a way to do this more elegant than traversing all objects by hand and restructure them.
  Was looking into <a href="https://www.npmjs.com/package/reorient" rel="nofollow noreferrer">reorient</a> and <a href="https://www.npmjs.com/package/obstruction" rel="nofollow noreferrer">obstruction</a> but can't find a way to get it done....</p>
  
  <p>Any help is appreciated!</p>
  
  <p>I know the pieces above are JSON strings that are read from file and then parsed to an object.</p>
  
  <p>The code I have to do this is currently(its not finished, because I didn't know what would be the best way to do the <code>remapJson()</code>:</p>
  
  <pre class="lang-js prettyprint-override"><code>var fs = require('fs'),
  jsonfile = require('jsonfile');
  
  function remapJson(oldData) {
    var newData = {};
  
    // Do the convertion (loop all keys and values?)
  
  
    return newData
  }
  
  obj = jsonfile.readFileSync('oldstructure.json');
  
  jsonfile.writeFileSync('newstructure.json', remapJson(obj));
  </code></pre>