There is a specific object that I am working with:
{
"images": [
{
"key": "ASDV1-01.jpg",
"image_location": "image1.jpg",
"data": {
"documentid": "CE44DBAC-59B2-4178-8392-0141FB2F58DF",
"scandate": "Feb 1 2018 12:05PM",
"F08": "1",
"F09": "",
"F10": "101076",
"F11": ""
},
"crops": {
"F08": {
"rectangle": {
"left": 690,
"top": 2111,
"width": 597,
"height": 121
}
},
"F09": {},
"F10": {
"rectangle": {
"left": 653,
"top": 821,
"width": 653,
"height": 243
}
},
"F11": {}
}
},
{
"key": "ASDV1-01.jpg",
"image_location": "image.png",
"crops": {
"F05": {
"rectangle": {
"left": 0,
"top": 808,
"width": 624,
"height": 243
}
}
},
"metadata": [
{
"name": "colors",
"data": {
"dimensions": {
"width": 2000,
"height": 2600
},
"coordinates": {
"width": {
"x": {
"lat": 4,
"long": [12, 345]
},
"y": {
"lat": {
"x" : [12,345],
"y": "234"
},
"long": 123
}
}
}
}
}
]
},
{
"key": "ASDV1-02.jpg",
"image_location": "image.png"
}
]
}
My goal is to switch the keys and the values of this object. This is how I imagine it would look like:
"ASDV1-01.jpg": "key",
"image.jpg": "image_location",
"data": {
"CE44DBAC-59B2-4178-8392-0141FB2F58DF": "documentid",
"Feb 1 2018 12:05PM": "scandate",
"1": "F08",
"101076": "F10",
I have attempted to achieve this using JavaScript functions without success. Do you have any advice on how I can properly display the result as a JSON string?
function swap(json){
var ret = {};
for(var key in json){
ret[json[key]] = key;
}
return ret;
}
var result = swap(data)
console.log(JSON.stringify(result, null, 2));