This is not the duplicate ticket.
I have checked the similar threads, like:
Parsing JSON with Python: TypeError: list indices must be integers, not str
Python Extract Value from Json
But none of them work for me.
The background of my question:
I use the REST API Groups - Get Groups with filter:
GET https://api.powerbi.com/v1.0/myorg/groups?$filter={$filter}&$top={$top}&$skip={$skip}
My code:
import json, requests, pandas as pd
try:
from azure.identity import ClientSecretCredential
except Exception:
# !pip install azure.identity
from azure.identity import ClientSecretCredential
tenant = 'xxxxxxxxxx'
client = 'yyyyyyyyyyy'
client_secret = 'zzzzzzzzzzzzzz'
api = 'https://analysis.windows.net/powerbi/api/.default'
# Generates the access token for the Service Principal
auth = ClientSecretCredential(authority = 'https://login.microsoftonline.com/',
tenant_id = tenant,
client_id = client,
client_secret = client_secret)
access_token = auth.get_token(api)
access_token = access_token.token
print('\nSuccessfully authenticated.')
base_url = 'https://api.powerbi.com/v1.0/myorg/'
header = {'Authorization': f'Bearer {access_token}'}
base_url_expand = f'{base_url}groups?$filter=name%20eq%20%27TestName%27'
# HTTP GET Request
groups = requests.get(base_url_expand, headers=header)
# Response code (200 = Success; 401 = Unauthorized; 404 = Bad Request)
print(groups)
And the result of the groups is <Response [200]>.
Then I want to get the id based on the name in the Response body:
So, I use the following the code to get the content of the groups:
try:
groups = json.loads(groups.content)
# Pretty-prints the JSON
print(json.dumps(groups, indent=4, sort_keys=True))
except Exception as e:
print('\nRequest failed:', e)
The print result is:
{
"@odata.context": "http://wabi-south-east-asia-redirect.analysis.windows.net/v1.0/myorg/$metadata#groups",
"@odata.count": 1,
"value": [
{
"id": "bf8f466d-35b0-4620-a11e-xxxxxxx",
"isOnDedicatedCapacity": false,
"isReadOnly": false,
"name": "TestName",
"type": "Workspace"
}
]
}
However, I could not extract value id from the Json file.

type(data)?