Skip to content
Advertisement

convert nested object to csv where all parents of lowest properties are visible on individual lines

I have a nested object that I want to to transform to flat lines. Below is the object:

    {
      "United States": {
        "New York": {
          "first": 11,
          "second": 7
        },
        "New Jersey": {
          "first": 8,
          "second": 2
        },
        "Pennsylvania": {
          "first": 4,
          "second": 2
        }
      }
    },
    {
      "South America": {
        "Brazil": {
          "first": 5,
          "second": 4
        }
      }
    },
    {
      "Africa": {
        "Zaire": {
          "first": 2,
          "second": 1
        }
      }
    },
    {
      "Asia": {
        "China": {
          "first": 10,
          "second": 4
        },
        "Japan": {
          "first": 6,
          "second": 3
        }
      }
    },
    {
      "Eastern Europe": {
        "Ukraine": {
          "first": 2,
          "second": 1
        }
      }
    },
    {
      "Europe": {
        "France": {
          "first": 2,
          "second": 4
        },
        "Germany": {
          "first": 1,
          "second": 7
        },
        "Portugal": {
          "first": 3,
          "second": 1
        },
        "Spain": {
          "first": 5,
          "second": 2
        },
        "Switzerland": {
          "first": 1,
          "second": 3
        }
      }
    }

I want to be able to see it like this, where the top level of the nest is always visible for each line:

    "United States", "New York", 11, 7
    "United States", "New Jersey", 8, 2
    "United States", "Pennsylvania", 4, 2
    "South America":, "Brazil", 5, 4
    "Africa", "Zaire", 2, 1
    "Asia", "China", 10, 4
    "Asia", "Japan", 6, 3
    "Eastern Europe", "Ukraine", 2, 1
    "Europe", "France", 2, 4
    "Europe", "Germany", 1, 7
    "Europe", "Portugal", 3, 1
    "Europe", "Spain", 5, 2
    "Europe", "Switzerland", 1, 3

I know how to loop through arrays to do this, but I’m not sure how to achieve it with a nested object. I’ve searched s/o, but haven’t seen anything that quite achieves this. (My actual data set is much longer) Any help is very welcome. Thank you.

Advertisement

Answer

Just perform a loop at each level of your structure:

let data = [{"United States": {"New York": {"first": 11,"second": 7},"New Jersey": {"first": 8,"second": 2},"Pennsylvania": {"first": 4,"second": 2}}},{"South America": {"Brazil": {"first": 5,"second": 4}}},{"Africa": {"Zaire": {"first": 2,"second": 1}}},{"Asia": {"China": {"first": 10,"second": 4},"Japan": {"first": 6,"second": 3}}},{"Eastern Europe": {"Ukraine": {"first": 2,"second": 1}}},{"Europe": {"France": {"first": 2,"second": 4},"Germany": {"first": 1,"second": 7},"Portugal": {"first": 3,"second": 1},"Spain": {"first": 5,"second": 2},"Switzerland": {"first": 1,"second": 3}}}];

for (let obj of data) {
    for (let [region, countries] of Object.entries(obj)) {
        for (let [country, indicators] of Object.entries(countries)) {
            console.log(region, country, ...Object.values(indicators));
        }
    }
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement