I am trying to reassign the following JSON structure, which is formatted by category, and then for each category may contain multiple locations. Location contains lon / lat and area 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"}
}
]
}
In the next structure, which is mainly focused on the areacode, then there are categories with actual locations in them.
{
"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}}
]}
]}
]
}
I am trying to do this in Javascript / Node and have looked at a way to make it more elegant than moving all the objects manually and restructuring them. I watched reorient and obstruction but can not find a way to do it ....
Any help is appreciated!
I know that the above snippets are JSON strings that are read from a file and then parsed to an object.
, , ( , , remapJson():
var fs = require('fs'),
jsonfile = require('jsonfile');
function remapJson(oldData) {
var newData = {};
return newData
}
obj = jsonfile.readFileSync('oldstructure.json');
jsonfile.writeFileSync('newstructure.json', remapJson(obj));