duc hathaway
duc hathaway

Reputation: 467

Pulling info from Json data

Problem:

I am having trouble pulling some info off ups. The city and state in the shipToAddress section.

Below is the data in a easy to read format that i am pulling from ups website with requests:

Data:

data = {
    'statusCode': '200',
    'statusText': 'Successful',
    'isLoggedInUser': False,
    'trackedDateTime': '04/16/2019 1:33 P.M. EST',
    'isBcdnMultiView': False,
    'trackDetails': [{
        'errorCode': None,
        'errorText': None,
        'requestedTrackingNumber': '1Z3774E8YN99957400',
        'trackingNumber': '1Z3774E8YN99957400',
        'isMobileDevice': False,
        'packageStatus': 'Loaded on Delivery Vehicle',
        'packageStatusType': 'I',
        'packageStatusCode': '072',
        'progressBarType': 'InTransit',
        'progressBarPercentage': '90',
        'simplifiedText': '',
        'scheduledDeliveryDayCMSKey': 'cms.stapp.tue',
        'scheduledDeliveryDate': '04/16/2019',
        'noEstimatedDeliveryDateLabel': None,
        'scheduledDeliveryTime': 'cms.stapp.eod',
        'scheduledDeliveryTimeEODLabel': 'cms.stapp.eod',
        'packageCommitedTime': '',
        'endOfDayResCMSKey': None,
        'deliveredDayCMSKey': '',
        'deliveredDate': '',
        'deliveredTime': '',
        'receivedBy': '',
        'leaveAt': None,
        'leftAt': '',
        'shipToAddress': {
            'streetAddress1': '',
            'streetAddress2': '',
            'streetAddress3': '',
            'city': 'OCEAN',
            'state': 'NJ',
            'province': None,
            'country': 'US',
            'zipCode': '',
            'companyName': '',
            'attentionName': '',
            'isAddressCorrected': False,
            'isReturnAddress': False,
            'isHoldAddress': False,
}}]}

Code:

data = response.text
addressinfo =json.loads(data)['trackDetails']['shipToAddress']

for entry in addressinfo:
    city = (entry['city'])  
    state = (entry['state'])
    country = (entry['country'])

My Expected Results:

city = 'Ocean'

state = 'NJ'

etc

this is error:

addressinfo =json.loads(data2)['trackDetails']['shipToAddress']

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

Upvotes: 0

Views: 91

Answers (2)

Green Cloak Guy
Green Cloak Guy

Reputation: 24711

Note the format of your JSON:

'trackDetails': [{
    ...
    'shipToAddress': {...}
}]

The dict you're trying to index into is actually contained inside of a list (note the square brackets). The proper way to access the shipToAddress field would be to do this:

addressinfo = json.loads(data2)['trackDetails'][0]['shipToAddress']
                                               ^^^

instead of what you were doing.

Upvotes: 4

MyNameIsCaleb
MyNameIsCaleb

Reputation: 4489

When you return data = response.text you should instead do data = response.json() since it is a json. This will allow you to access it like a json. Instead you are converting it to a string with .text and then attempting to load it back in which is not necessary.

Then access city:

city = data['trackDetails'][0]['shipToAddress']['city']
state = data['trackDetails'][0]['shipToAddress']['state']

Upvotes: 1

Related Questions