-3

I have the following data in a .json file:

{
    "countries": {
        "sweden": {
            "currency": "Swedish krona",
            "majorLanguage": "Swedish",
            "landArea": {               
                "value": 410330,
                "uom": "sq km"
                }           
            },
        "japan": {
            "currency": "yen",
            "majorLanguage": "Japanese",
            "landArea": {
                "value": 364500,
                "uom": "sq km"
                }           
            },
        "unitedStatesOfAmerica": {
            "currency": "US dollar",
            "majorLanguage": "English",
            "landArea": {
                "value": 3796742,
                "uom": "sq mi"
                }
            }
    }
}

and need to come up with a way to create this object from it:

Object {
    "currency": Object {
    "japan": "yen",
    "sweden": "Swedish krona",
    "unitedStatesOfAmerica": "US dollar"
  },
  "majorLanguage": Object {
    "japan": "Japanese",
    "sweden": "Swedish",
    "unitedStatesOfAmerica": "English"
  },
  "landArea": Object {
    "japan": Object {
      "value": 364500,
      "uom": "sq km"
    },
    "sweden": Object {
      "value": 410330,
      "uom": "sq km"
    },
    "unitedStatesOfAmerica": Object {
      "value": 3796742,
      "uom": "sq mi"
    }
  }
}

The app that will be consuming this data is written in Vue so using JavaScript to accomplish this would make sense, although my preference is to not use any third party libraries. Specifically, I'm interested in a programmatic approach that doesn't require hard coding of to manually create objects for currency, majorLanguage, landArea. I don't really know how to start tackling this so don't have any sample attempts to post here.

knot22
  • 2,648
  • 5
  • 31
  • 51
  • 3
    https://meta.stackoverflow.com/q/284236/3001761 – jonrsharpe Mar 29 '20 at 17:25
  • Have a look on this link: https://stackoverflow.com/questions/45565349/how-to-acces-external-json-file-objects-in-vue-js-app once you load the file, it is converted to a JS object, therefore to reach the final object you will need to do some operations over it – Claudio Busatto Mar 29 '20 at 17:29
  • JSON.stringify(json) have you tried this one? – Vanns35 Mar 29 '20 at 17:49

2 Answers2

1

You can use Array#reduce method and do something like the following.

const data = {"countries":{"sweden":{"currency":"Swedish krona","majorLanguage":"Swedish","landArea":{"value":410330,"uom":"sq km"}},"japan":{"currency":"yen","majorLanguage":"Japanese","landArea":{"value":364500,"uom":"sq km"}},"unitedStatesOfAmerica":{"currency":"US dollar","majorLanguage":"English","landArea":{"value":3796742,"uom":"sq mi"}}}};
            // iterate over countries object key value pair
const res = Object.entries(data.countries).reduce((obj, [country, valObj]) => {
  // iterate over the country value key-val pair
  Object.entries(valObj).forEach(([key, val]) => {
    // define key if not defined
    obj[key] = obj[key] || {};
    // define the value within object where key is country
    obj[key][country] = val;
  })
  return obj;
}, {})

console.log(res);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
1

Nothing fancy here:

const result = {};

for (const name in countries) {
    const country = countries[name];
    for (const key in country) {
        if (!result[key]) result[key] = {};
        result[key][name] = country[key];
    }
}
Rubydesic
  • 3,386
  • 12
  • 27