I make use of an API which outputs me adresses. However the adresses are nested as such:
{
"totalItemCount":55,
"pageCount":1,
"size":100,
"_links":{
"self":{
"href":"\/bag?filters[postcode]=1011PL&ovio-api-key=KEY"
}
},
"_embedded":{
"adres":[
{
"huisnummer":"7",
"huisletter":"",
"postcode":"1011PL",
"huisnummertoevoeging":"",
"openbareruimte":"Nieuwe Amstelstraat",
"slug":"1011pl-nieuwe-amstelstraat-7",
"woonplaats":"Amsterdam",
"_links":{
"self":{
"href":"\/bag\/1011pl-nieuwe-amstelstraat-7"
}
}
},
{
"huisnummer":"25",
"huisletter":"",
"postcode":"1011PL",
"huisnummertoevoeging":"",
"openbareruimte":"Nieuwe Amstelstraat",
"slug":"1011pl-nieuwe-amstelstraat-25",
"woonplaats":"Amsterdam",
"_links":{
"self":{
"href":"\/bag\/1011pl-nieuwe-amstelstraat-25"
}
}
},
My current script:
## Convert Output JSON to CSV
f = open("output.json", "r+")
x = json.loads(f.read())
f.close()
# print(x['_embedded']['adres'][0]['openbareruimte'])
f = csv.writer(open("test.csv", "w"))
f.writerow(["straat","huisnummer","postcode","stad"])
for y in x:
f.writerow([x["_embedded"]["adres"][0]["openbareruimte"],
x["_embedded"]["adres"][0]["huisnummer"],
x["_embedded"]["adres"][0]["postcode"],
x["_embedded"]["adres"][0]["woonplaats"]])
I want to output all of the streets, numbers, postal codes and cities to CSV, but it only outputs the first adress. I have tried using split and format but I'm too unfamiliar with that. If anyone knows how to make use of the nested data, it would be appreciated. I could not find any tutorial in regards.