简体   繁体   中英

How to read fields without numeric index in JSON

I have a json file where I need to read it in a structured way to insert in a database each value in its respective column, but in the tag "customFields" the fields change index, example: "Tribe / Customer" can be index 0 (row['customFields'][0]) in a json block, and in the other one be index 3 (row['customFields'][3]) , so I tried to read the data using the name of the row field ['customFields'] ['Tribe / Customer'] , but I got the error below:

TypeError: list indices must be integers or slices, not str

Script:

def getCustomField(ModelData):

    for row in ModelData["data"]["squads"][0]["cards"]:

        print(row['identifier'],
              row['customFields']['Tribe / Customer'],
              row['customFields']['Stopped with'],
              row['customFields']['Sub-Activity'],
              row['customFields']['Activity'],
              row['customFields']['Complexity'],
              row['customFields']['Effort'])
            
    
if __name__ == "__main__":
    f = open('test.json')
    json_file = json.load(f)
    getCustomField(json_file)

JSON:

{
    "data": {
        "squads": [
            {
                "name": "TESTE",
                "cards": [
                    {
                        "identifier": "0102",
                        "title": "TESTE",
                        "description": " TESTE ",
                        "status": "on_track",
                        "priority": null,
                        "assignees": [
                            {
                                "fullname": "TESTE",
                                "email": "TESTE"
                            }
                        ],
                        "createdAt": "2020-04-16T15:00:31-03:00",
                        "secondaryLabel": null,
                        "primaryLabels": [
                            "TESTE",
                            "TESTE"
                        ],
                        "swimlane": "TESTE",
                        "workstate": "Active",
                        "customFields": [
                            {
                                "name": "Tribe / Customer",
                                "value": "TESTE 1"
                            },
                            {
                                "name": "Checkpoint",
                                "value": "GNN"
                            },
                            {
                                "name": "Stopped with",
                                "value": null
                            },
                            {
                                "name": "Sub-Activity",
                                "value": "DEPLOY"
                            },
                            {
                                "name": "Activity",
                                "value": "TOOL"
                            },
                            {
                                "name": "Complexity",
                                "value": "HIGH"
                            },
                            {
                                "name": "Effort",
                                "value": "20"
                            }
                        ]
                    },
                    {
                        "identifier": "0103",
                        "title": "TESTE",
                        "description": " TESTE ",
                        "status": "on_track",
                        "priority": null,
                        "assignees": [
                            {
                                "fullname": "TESTE",
                                "email": "TESTE"
                            }
                        ],
                        "createdAt": "2020-04-16T15:00:31-03:00",
                        "secondaryLabel": null,
                        "primaryLabels": [
                            "TESTE",
                            "TESTE"
                        ],
                        "swimlane": "TESTE",
                        "workstate": "Active",
                        "customFields": [
                            {
                                "name": "Tribe / Customer",
                                "value": "TESTE 1"
                            },
                            {
                                "name": "Stopped with",
                                "value": null
                            },
                            {
                                "name": "Checkpoint",
                                "value": "GNN"
                            },
                            {
                                "name": "Sub-Activity",
                                "value": "DEPLOY"
                            },
                            {
                                "name": "Activity",
                                "value": "TOOL"
                            },
                            {
                                "name": "Complexity",
                                "value": "HIGH"
                            },
                            {
                                "name": "Effort",
                                "value": "20"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

You'll have to parse the list of custom fields into something you can access by name. Since you're accessing multiple entries from the same list, a dictionary is the most appropriate choice.

for row in ModelData["data"]["squads"][0]["cards"]:
    custom_fields_dict = {field['name']: field['value'] for field in row['customFields']}
    print(row['identifier'],
          custom_fields_dict['Tribe / Customer'],
          ...
         )

If you only wanted a single field you could traverse the list looking for a match, but it would be less efficient to do that repeatedly.

I'm skipping over dealing with missing fields - you'd probably want to use get('Tribe / Customer', some_reasonable_default) if there's any possibility of the field not being present in the json list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM