I need help because I'm using an API that gives me a result like that
{
"header": {
"statut": 200,
"message": "OK"
},
"uniteLegale": {
"siren": "410032460",
"statutDiffusionUniteLegale": "O",
"dateCreationUniteLegale": "1997-01-01",
"sigleUniteLegale": null,
"sexeUniteLegale": null,
"prenom1UniteLegale": null,
"prenom2UniteLegale": null,
"prenom3UniteLegale": null,
"prenom4UniteLegale": null,
"prenomUsuelUniteLegale": null,
"pseudonymeUniteLegale": null,
"identifiantAssociationUniteLegale": null,
"trancheEffectifsUniteLegale": "32",
"anneeEffectifsUniteLegale": "2020",
"dateDernierTraitementUniteLegale": "2022-08-29T09:27:06",
"nombrePeriodesUniteLegale": 6,
"categorieEntreprise": "ETI",
"anneeCategorieEntreprise": "2020",
"periodesUniteLegale": [{
"dateFin": null,
"dateDebut": "2020-12-31",
"etatAdministratifUniteLegale": "A",
"changementEtatAdministratifUniteLegale": false,
"nomUniteLegale": null,
"changementNomUniteLegale": false,
"nomUsageUniteLegale": null,
"changementNomUsageUniteLegale": false,
"denominationUniteLegale": "K. LINE",
"changementDenominationUniteLegale": false,
"denominationUsuelle1UniteLegale": null,
"denominationUsuelle2UniteLegale": null,
"denominationUsuelle3UniteLegale": null,
"changementDenominationUsuelleUniteLegale": false,
"categorieJuridiqueUniteLegale": "5710",
"changementCategorieJuridiqueUniteLegale": false,
"activitePrincipaleUniteLegale": "46.73A",
"nomenclatureActivitePrincipaleUniteLegale": "NAFRev2",
"changementActivitePrincipaleUniteLegale": false,
"nicSiegeUniteLegale": "00017",
"changementNicSiegeUniteLegale": false,
"economieSocialeSolidaireUniteLegale": "N",
"changementEconomieSocialeSolidaireUniteLegale": true,
"caractereEmployeurUniteLegale": "O",
"changementCaractereEmployeurUniteLegale": false,
"societeMissionUniteLegale": null,
"changementSocieteMissionUniteLegale": false
}, {
"dateFin": "2020-12-30",
"dateDebut": "2008-01-01",
"etatAdministratifUniteLegale": "A",
"changementEtatAdministratifUniteLegale": false,
"nomUniteLegale": null,
"changementNomUniteLegale": false,
"nomUsageUniteLegale": null,
"changementNomUsageUniteLegale": false,
"denominationUniteLegale": "K. LINE",
"changementDenominationUniteLegale": false,
"denominationUsuelle1UniteLegale": null,
"denominationUsuelle2UniteLegale": null,
"denominationUsuelle3UniteLegale": null,
"changementDenominationUsuelleUniteLegale": false,
"categorieJuridiqueUniteLegale": "5710",
"changementCategorieJuridiqueUniteLegale": false,
"activitePrincipaleUniteLegale": "46.73A",
"nomenclatureActivitePrincipaleUniteLegale": "NAFRev2",
"changementActivitePrincipaleUniteLegale": true,
"nicSiegeUniteLegale": "00017",
"changementNicSiegeUniteLegale": false,
"economieSocialeSolidaireUniteLegale": null,
"changementEconomieSocialeSolidaireUniteLegale": false,
"caractereEmployeurUniteLegale": "O",
"changementCaractereEmployeurUniteLegale": false,
"societeMissionUniteLegale": null,
"changementSocieteMissionUniteLegale": false
}, {
"dateFin": "2007-12-31",
"dateDebut": "2003-12-25",
"etatAdministratifUniteLegale": "A",
"changementEtatAdministratifUniteLegale": false,
"nomUniteLegale": null,
"changementNomUniteLegale": false,
"nomUsageUniteLegale": null,
"changementNomUsageUniteLegale": false,
"denominationUniteLegale": "K. LINE",
"changementDenominationUniteLegale": false,
"denominationUsuelle1UniteLegale": null,
"denominationUsuelle2UniteLegale": null,
"denominationUsuelle3UniteLegale": null,
"changementDenominationUsuelleUniteLegale": false,
"categorieJuridiqueUniteLegale": "5710",
"changementCategorieJuridiqueUniteLegale": false,
"activitePrincipaleUniteLegale": "51.5F",
"nomenclatureActivitePrincipaleUniteLegale": "NAFRev1",
"changementActivitePrincipaleUniteLegale": true,
"nicSiegeUniteLegale": "00017",
"changementNicSiegeUniteLegale": false,
"economieSocialeSolidaireUniteLegale": null,
"changementEconomieSocialeSolidaireUniteLegale": false,
"caractereEmployeurUniteLegale": "O",
"changementCaractereEmployeurUniteLegale": false,
"societeMissionUniteLegale": null,
"changementSocieteMissionUniteLegale": false
}, {
"dateFin": "2003-12-24",
"dateDebut": "2001-12-25",
"etatAdministratifUniteLegale": "A",
"changementEtatAdministratifUniteLegale": false,
"nomUniteLegale": null,
"changementNomUniteLegale": false,
"nomUsageUniteLegale": null,
"changementNomUsageUniteLegale": false,
"denominationUniteLegale": "K. LINE",
"changementDenominationUniteLegale": false,
"denominationUsuelle1UniteLegale": null,
"denominationUsuelle2UniteLegale": null,
"denominationUsuelle3UniteLegale": null,
"changementDenominationUsuelleUniteLegale": false,
"categorieJuridiqueUniteLegale": "5710",
"changementCategorieJuridiqueUniteLegale": true,
"activitePrincipaleUniteLegale": null,
"nomenclatureActivitePrincipaleUniteLegale": null,
"changementActivitePrincipaleUniteLegale": false,
"nicSiegeUniteLegale": "00017",
"changementNicSiegeUniteLegale": false,
"economieSocialeSolidaireUniteLegale": null,
"changementEconomieSocialeSolidaireUniteLegale": false,
"caractereEmployeurUniteLegale": "O",
"changementCaractereEmployeurUniteLegale": false,
"societeMissionUniteLegale": null,
"changementSocieteMissionUniteLegale": false
}, {
"dateFin": "2001-12-24",
"dateDebut": "1997-12-25",
"etatAdministratifUniteLegale": "A",
"changementEtatAdministratifUniteLegale": false,
"nomUniteLegale": null,
"changementNomUniteLegale": false,
"nomUsageUniteLegale": null,
"changementNomUsageUniteLegale": false,
"denominationUniteLegale": "K. LINE",
"changementDenominationUniteLegale": false,
"denominationUsuelle1UniteLegale": null,
"denominationUsuelle2UniteLegale": null,
"denominationUsuelle3UniteLegale": null,
"changementDenominationUsuelleUniteLegale": false,
"categorieJuridiqueUniteLegale": "5599",
"changementCategorieJuridiqueUniteLegale": false,
"activitePrincipaleUniteLegale": null,
"nomenclatureActivitePrincipaleUniteLegale": null,
"changementActivitePrincipaleUniteLegale": false,
"nicSiegeUniteLegale": "00017",
"changementNicSiegeUniteLegale": false,
"economieSocialeSolidaireUniteLegale": null,
"changementEconomieSocialeSolidaireUniteLegale": false,
"caractereEmployeurUniteLegale": "O",
"changementCaractereEmployeurUniteLegale": true,
"societeMissionUniteLegale": null,
"changementSocieteMissionUniteLegale": false
}, {
"dateFin": "1997-12-24",
"dateDebut": "1997-01-01",
"etatAdministratifUniteLegale": "A",
"changementEtatAdministratifUniteLegale": false,
"nomUniteLegale": null,
"changementNomUniteLegale": false,
"nomUsageUniteLegale": null,
"changementNomUsageUniteLegale": false,
"denominationUniteLegale": "K. LINE",
"changementDenominationUniteLegale": false,
"denominationUsuelle1UniteLegale": null,
"denominationUsuelle2UniteLegale": null,
"denominationUsuelle3UniteLegale": null,
"changementDenominationUsuelleUniteLegale": false,
"categorieJuridiqueUniteLegale": "5599",
"changementCategorieJuridiqueUniteLegale": false,
"activitePrincipaleUniteLegale": null,
"nomenclatureActivitePrincipaleUniteLegale": null,
"changementActivitePrincipaleUniteLegale": false,
"nicSiegeUniteLegale": "00017",
"changementNicSiegeUniteLegale": false,
"economieSocialeSolidaireUniteLegale": null,
"changementEconomieSocialeSolidaireUniteLegale": false,
"caractereEmployeurUniteLegale": null,
"changementCaractereEmployeurUniteLegale": false,
"societeMissionUniteLegale": null,
"changementSocieteMissionUniteLegale": false
}]
}
}
I need with python to take only few elements inside it : uniteLegale : "siren", "categorieEntreprise", "trancheEffectifsUniteLegale"; periodesUniteLegale : "denominationUniteLegale".
For this I do that :
data = json.loads(api_call_response.text) # data = my json answer
siren = data['uniteLegale']['siren']
categorieEntreprise = data['uniteLegale']['categorieEntreprise']
effectif = data['uniteLegale']['trancheEffectifsUniteLegale']
periodesUnite = data['uniteLegale']['periodesUniteLegale']
denomination = periodeUnite['denominationUniteLegale']
but I got the error : denomination = periodeUnite['denominationUniteLegale'] TypeError: list indices must be integers or slices, not str
Python thinks that periodesUnite is a list and not a dictionnary so I can't get my data.
What can I do ?
Thanks you
CodePudding user response:
It is a list:
See the square brackets. Your JSON sample
periodesUnite is extracted from data['uniteLegale']['periodesUniteLegale']. As your key 'periodesUniteLegale' is a list so as your var periodesUnite.
CodePudding user response:
Since it is indeed a list, one way to make sure you capture all the values is to use a list comprehension that takes the desired key from each dictionary:
>>> [d["denominationUniteLegale"] for d in periodesUnite]
['K. LINE', 'K. LINE', 'K. LINE', 'K. LINE', 'K. LINE', 'K. LINE']
If you use a set comprehension instead, you get only the unique values (of which there is only one):
>>> {d["denominationUniteLegale"] for d in periodesUnite}
{'K. LINE'}
So you could replace this line:
denomination = periodeUnite['denominationUniteLegale']
with a line that gets the multiple values, e.g.:
denominations = {d["denominationUniteLegale"] for d in periodesUnite}
If you're confident that there will only ever be one value and you want to get that one value, you might do:
denominations = {d["denominationUniteLegale"] for d in periodesUnite}
denomination = denominations.pop()
assert not denominations, f"Extra denominations: {denominations}!"