R
R
Razer15112020-12-03 10:31:06
Python
Razer1511, 2020-12-03 10:31:06

How to unpack complex json responses via python?

Good afternoon. Please tell me how to go through the for loop through a complex json response consisting of several blocks.

Example json response:
data =

[{"LocationId":1,"BlastHole":[{"Coords":{"X":15599.1298828125,"Y":35847.578125,"Z":180.10580444335938},"DesignMass":835.0,"AdditionalCharge":0.0,"RealAdditionalMass":0.0,"DesignHeight":9.992707,"RealMass":1301.0,"DesignExplosiveId":8,"RealHeight":15.56948,"RealExplosiveId":8,"Depth":10.0,"SourceHoleDepth":17.10579,"Tamping":0.0,"StatusId":4,"UnitId":4062,"StaffId":1019,"SecondStaffId":-1,"ShiftId":1,"Diameter":0.311,"StartTimestamp":"2020-08-18T08:46:39","EndTimeStamp":"2020-08-18T08:47:16","HoleNotes":null,"HoseLength":0.0,"BlockID":14999,"ID":717689,"Name":"100","OldName":null} . . . {} . . . {} . . . {}]


You need to find out the value of the variable from the top json response, or at least the line number in which the loop condition is located.
My version of the code:

for i in data:
    if i['BlastHole']['Name'] == '100':
        print(['BlastHole']['SecondStaffId'])


Error:

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

PS I know that in this case it will work through , but there can be 300+ such lines, and I need to find out exactly the value of the variable. i['BlastHole'][0]['Name']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bbkmzzzz, 2020-12-03
@bbkmzzzz

spoiler

data = [
    {
        "LocationId": 1, "BlastHole":
        [
            {
                "Coords":
                    {
                        "X": 15599.1298828125, "Y": 35847.578125, "Z": 180.10580444335938
                    },
                "DesignMass": 835.0,
                "AdditionalCharge": 0.0,
                "RealAdditionalMass": 0.0,
                "DesignHeight": 9.992707,
                "RealMass": 1301.0,
                "DesignExplosiveId": 8,
                "RealHeight": 15.56948,
                "RealExplosiveId": 8,
                "Depth": 10.0,
                "SourceHoleDepth": 17.10579,
                "Tamping": 0.0,
                "StatusId": 4,
                "UnitId": 4062,
                "StaffId": 1019,
                "SecondStaffId": -1,
                "ShiftId": 1,
                "Diameter": 0.311,
                "StartTimestamp": "2020-08-18T08:46:39",
                "EndTimeStamp": "2020-08-18T08:47:16",
                "HoleNotes": "null",
                "HoseLength": 0.0,
                "BlockID": 14999,
                "ID": 717689,
                "Name": "100",
                "OldName": "null"
            }
        ]
    }
]


+ 1 print, and immediately clearer
for i in data:
    print('BlastHole', i['BlastHole'])
    if i['BlastHole']['Name'] == '100':
        print(['BlastHole']['SecondStaffId'])

The list element has a numeric index, no keys, so there is only one access option. What lines can be many? Go through the list one more time? i['BlastHole'][0]['Name']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question