I'm querying an API and would like to store the results of that API, the JSON response looks something like this:
{
"name": "John Smith",
"id": 123456,
"geo": {
"region": {
"city": false
},
"country": "United States",
"ethnicity": "White",
}
}
The dictionary I'm trying to parse it into looks like this:
dictP = {
"name": "",
"id": 0,
"geo.region.city": "",
"geo.country": "",
"geo.ethnicity": ""
}
There's a lot more data, about 120 data points being returned, lots of nested/non nested; so I'm excluding a big chunk of data thats useless to me. Only extracting what i need. The issue is sometimes the data is missing, IE:
{
"name": "John Smith",
"id": 123456,
"geo": {
"region": null,
"country": "United States",
"ethnicity": "White",
}
}
and:
{
"name": "John Smith",
"id": 123456,
"geo": null
}
or:
{
"name": "John Smith",
"id": null,
"geo": null
}
What's the best way to parse this? I have about 75 data points i want to parse, writing if/else statements or try/except statements 75 times does not make sense. The data needs to all be uniform because I'm saving to CSV, so ultimately I'd like to fill "None" for missing data, i can't seem to find a library that does this. Advice appreciated.