Home > Software engineering >  Python Json value overwritten by last value
Python Json value overwritten by last value

Time:03-06

lista =
[{Identity: joe,
summary:[
  {distance: 1, time:2, status: idle},
  {distance:2, time:5, status: moving}],
{unit: imperial}]

I can pull the data easily and put in pandas. The issue is, if an identity has multiple instances of, say idle, it takes the last value, instead of summing together.

actual link for JSON: https://rdc-prod-ws.aws.roadnet.com/OperationWebService.svc/help/operations/GetAllOperationProfile#response-json

my code...

zdrivershours = {}
zdistance = {}
zstophours = {}

For driver in resp:
   driverid[driver['AssetID']] = driver['AssetName']
      for value in [driver['SegmentSummary']]:
         for value in value:
           if value['SegmentType'] == 'Motion':
             zdriverhours[driver['AssetID']] = round(value['Time']/3600,2)
           if value['SegmentType'] == 'Stop':
             zstophours[driver['AssetID']] = round(value['IdleTime']/3600,2)
           zdistance[driver['AssetID']] = value['Distance']

CodePudding user response:

To obtain the summatory of distance for every driver replace:

zdistance[driver['AssetID']] = value['Distance']

by

if driver['AssetID'] in zdistance:
    zdistance[driver['AssetID']] = zdistance[driver['AssetID']]   value['Distance']
else:
    zdistance[driver['AssetID']] = value['Distance']
  • Related