The json I'm trying to parse has a complex structure. Within the full list, there are 311 dictionaries, from which I managed to get some key values. However, within each of those 311 dictionaries, there is a key 'cadCargosFuncoes', whose value is a dictionary with one key, whose value is itself a list of dictionaries.
What I need is, for each of those 311 dictionaries of the json, grab the key value 'funDes' within each of its n dictionaries, where n is not always the same, nor guaranteed to be different than zero.
I've tried to introduce a loop:
for i in ['cadCargosFuncoes']['pt_ar_wsgode_objectos_DadosCargosFuncoes']`
in some different ways, but was never able to achieve what I need.
import requests
import json
import csv
from csv import DictWriter
r = requests.get('http://app.parlamento.pt/webutils/docs/doc.txt?path=6148523063446f764c324679626d56304c3239775a57356b595852684c3052685a47397a51574a6c636e5276637939535a576470633352764a544977516d6c765a334c446f575a705932387657456c4a535355794d45786c5a326c7a6247463064584a684c314a6c5a326c7a644739436157396e636d466d61574e7657456c4a53563971633239754c6e523464413d3d&fich=RegistoBiograficoXIII_json.txt&Inline=true')
r.raise_for_status()
data = r.json()
mps = data['RegistoBiografico']['RegistoBiograficoList']['pt_ar_wsgode_objectos_DadosRegistoBiograficoWeb']
for j in mps:
if 'cadProfissao' in j:
pass
else:
j['cadProfissao'] = '-88'
for j in mps:
if 'cadDtNascimento' in j:
pass
else:
j['cadDtNascimento'] = '-88'
result = [{"ID": j["cadId"], "Nome": j["cadNomeCompleto"], "Sexo": j["cadSexo"], "D-Nasc": j["cadDtNascimento"], "Profissao": j["cadProfissao"]} for j in mps]
with open('bio_xiii.csv', 'w') as outfile:
writer = DictWriter(outfile, ('ID', 'Nome', 'Sexo', 'D-Nasc', 'Profissao'))
writer.writerows(result)
Currently, my csv file has 311 rows, with 'cadId', 'cadNomeCompleto', 'cadSexo', 'cadDtNascimento', and 'cadProfissao'. What I want is m additional columns, where m is the maximum number of dictionaries with the key 'funDes' for those 311 dictionaries, such that each cell contains the value of 'funDes' when such value exists, and '-88' when it doesn't exist (when it's either null or the maximum number of dictionaries with 'funDes' is lower than m, for some of those 311 dictionaries).
Can someone give some advice on how to work this out?