How can I unpack a nested JSON-file and normalize it to a Pandas dataframe?

Viewed 64

I have problems un-nesting a json file and converting it to a pandas dataframe, which can be seen below:

{
  "metadata": {
    "rowHierarchy": [
      "Nasjonalt",
      "Fylke",
      "Kommune",
      "Enhet"
    ],
    "columns": [
      [
        {
          "name": "2021-22",
          "tekstId_kolonne": "160015",
          "columnCount": 2
        }
      ],
      [
        {
          "name": "Gutt",
          "tekstId_kolonne": "160018",
          "columnCount": 1
        },
        {
          "name": "Jente",
          "tekstId_kolonne": "160018",
          "columnCount": 1
        }
      ],
      [
        {
          "name": "Alle eierformer",
          "tekstId_kolonne": "160019",
          "columnCount": 1
        },
        {
          "name": "Alle eierformer",
          "tekstId_kolonne": "160019",
          "columnCount": 1
        }
      ],
      [
        {
          "name": "Antall lærere",
          "tekstId_kolonne": "160014",
          "columnCount": 1
        },
        {
          "name": "Antall lærere",
          "tekstId_kolonne": "160014",
          "columnCount": 1
        }
      ]
    ]
  },
  "rows": [
    {
      "navn": "Hele landet",
      "id": "1",
      "data": [
        "17 797",
        "52 271"
      ],
      "childCount": 5
    },
    {
      "navn": "Agder",
      "id": "1.49",
      "data": [
        "1 123",
        "3 131"
      ],
      "childCount": 4
    },
    {
      "navn": "Arendal",
      "id": "1.49.1423",
      "data": [
        "144",
        "426"
      ],
      "childCount": 3
    },
    {
      "navn": "Asdal skole",
      "id": "1.49.1423.28041",
      "data": [
        "10",
        "40"
      ],
      "childCount": 0
    },
    {
      "navn": "Birkenlund skole",
      "id": "1.49.1423.28170",
      "data": [
        "11",
        "33"
      ],
      "childCount": 0
    },
    {
      "navn": "Eydehavn skole",
      "id": "1.49.1423.28474",
      "data": [
        "3",
        "9"
      ],
      "childCount": 0
    }
  ]
}

My goal is to re-create the csv output that one can get from the API:

enter image description here

And this is how the table from the GUI looks like:

enter image description here

I don't see how I can link or know what the keys are for "metadata" in the json to "rows".

If it is easier to recreate the problem with the url, https://statistikkportalen.udir.no/api/rapportering/rest/v1/Statistikk/GSK/GSI/3/1/data?radSti=F&filtre=EnhetID(-720_-538_-12_28041_28170_28474)_KjoennID(1_2)_KommunalitetID(-10)_SpoersmaalID(6_8_9_10)_TidID(202110)&dataChanged=2022-01-31_134759

0 Answers
Related