mardi 17 décembre 2019

if-Else condition working with JSON format in Python to determine which raws to append to a list

I am producing a dataset starting from a series of JSON files associated with a certain ID (authors_df contains a bunch of ids) and I am using for to do this. I tried with a subset of authors and it works fine.

The problem is that some of the id have have an incomplete Json file. Thus I tried to include some 'else' conditions to make the code work also with incomplete data (json files of length 0). the problem is that I don't know how to do.

I tried if len(json_value['resonanceCategorizations']['1']['fullData']) > 0 else null

but it does not work (KeyError: '1'). I guess I have to set a different condition encompassing JSON structure of the complete files rather than using null

here is my code, it all works but the problem is with the line with else null.

json_values_per_author = {}
datalist = []
datadict = {}

for index, row in authors_df.iterrows():
    #get the author
    author = row['author']
    print(author)

#build the url
url = f'http://keystone-db.default.svc.cluster.local:5000/keystonedb/profiles/resonance/categorization?profileId={author}&regionId=1'    

#get the json value
json_value = requests.get(url).json()
full_data = json_value['resonanceCategorizations']['1']['fullData'] if len(json_value['resonanceCategorizations']['1']['fullData']) > 0 else null

datalist.append({
    "author": author,

    "seed1": full_data[0]['seed'],

    "seed2": full_data[1]['seed'] if len(full_data) > 2 else 'NA',

    "seed3": full_data[2]['seed'] if len(full_data) > 3 else 'NA'

})

another thing I tried was

 z = {"000": [{"seed": 0, "globalSegmentId": 0, "globalSegmentName": "Nope", "regionId": 0, "resonance": 0, "isGlobal": true, "globalRegion": 1}]}


full_data = json_value['resonanceCategorizations']['1']['fullData'] if len(json_value['resonanceCategorizations']['1']['fullData']) > 0 else z

basically creating a "null" JSON value to input as a default if there is no data

alternatively, it would be fine if I could just avoid appending the authors with no data.

Aucun commentaire:

Enregistrer un commentaire