0

Hey there I am trying to extract items in an array inside a json dictionary but I'm getting the below error. Thank you in advance for your help.

Error:

    for r in data[p]['reportDetails']:
TypeError: unhashable type: 'dict'

The code snippet is below:

def XeroExtractReports(cred_fp):
    with open(cred_fp, 'r') as json_file:
        data = json.load(json_file)
        for p in data['credentials']:
            client_id = p['clientId']
            client_secret = p['clientSecret']
            old_refresh_token = p['refreshToken']

            for r in data[p]['reportDetails']:
                get_url = 'https://api.xero.com/api.xro/2.0/' + r['reportName']
                response = requests.get(get_url,
                                       headers = {
                                           'Authorization': 'Bearer ' + new_tokens[0],
                                           'Xero-tenant-id': xero_tenant_id,
                                           'Accept': 'application/json'
                                       })
                json_response = response.json()
                print(json_response)
                print('\n')

JSON Dictionary:

{
    "credentials": [{
        "clientName": "C1",
        "clientId": "null",
        "clientSecret": "null",
        "redirectUrl": "http://localhost:8080/callback",
        "scopes": "offline_access accounting.transactions.read",
        "reportType": "null",
        "refreshToken": "null",
        "reportDetails": [
            {
                "reportName": "BankTransactions",
                "reportFilename": "/BankTransactions.txt"
            },
            {
                "reportName": "BankTransfers",
                "reportFilename": "/BankTransfers.txt"
            }
        ]
    }]
}

2 Answers 2

4

you are trying to access values on a wrong way

instead of:

for r in data[p]['reportDetails']:
    ...

use:

for r in p['reportDetails']:
    ...
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to use data[p] to get value of reportDetails.

You already got the dict when you set p

Change the second loop with:

for r in p['reportDetails']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.