1

I'm trying to learn python and am wanting to use it to interact with the PowerBI API, specifically to check the current parameters of a dataset:

getParametersInGroup API

I've created a function that successfully returns the data I need, but I want to filter it and it is a bit beyond me..

    def check_dataset_parameter(access_token, parameter_name = "FarmId"):
    url = "https://api.powerbi.com/v1.0/myorg/groups/" + group_id + "/datasets/" + dataset_id + "/parameters"
    headers = {
        'content-type': "application/json",
        'authorization': "Bearer " + access_token,
    }
    response = requests.request("GET", url, data=None, headers=headers)
    response_full_json = response.json()
    response_value_str = str(response.json()['value'])
    parameter_list_clean = response_value_str.replace("'", '"').replace('[', '').replace(']', '').replace(' ', '')
    actual_value_i_want = response.json()['value'][0]['currentValue']
    current_value = actual_value_i_want ###Except I want the current value of the parameter specified in the function call
    return current_value

This code returns '179', as it is the currentValue at position 0. What I want is to return the current value for the parameter specified in the function call (FarmId or OrgId).

The output of parameter_list_clean is:

{"name":"FarmId","type":"Text","isRequired":True,"currentValue":"179"},{"name":"OrgId","type":"Number","isRequired":True,"currentValue":"-1"}

I made this to try and convert the string to a dict, but it is failing, I've also tried various forms of json.loads and json.dumps.

The raw output of response_full_json looks like this:

{'@odata.context': 'http://wabi-australia-southeast-redirect.analysis.windows.net/v1.0/myorg/groups/<my_group>/$metadata#parameters', 'value': [{'name': 'FarmId', 'type': 'Text', 'isRequired': True, 'currentValue': '179'}, {'name': 'OrgId', 'type': 'Number', 'isRequired': True, 'currentValue': '-1'}]}
Max xaM
  • 348
  • 2
  • 12

1 Answers1

0

I found an answer. The problem was the data returned by response.json()['value'] is actually a list containing two (or more) dictionaries. I managed to isolate the individual dictionary required using next():

    def check_dataset_parameter(access_token, parameter_name="FarmId"):
    url = "https://api.powerbi.com/v1.0/myorg/groups/" + group_id + "/datasets/" + dataset_id + "/parameters"
    headers = {
        'content-type': "application/json",
        'authorization': "Bearer " + access_token,
    }
    response = requests.request("GET", url, data=None, headers=headers)
    response_value = response.json()['value']
    parameter_dict = next((item for item in response_value if item["name"] == parameter_name), None)
    current_value = parameter_dict['currentValue']
    return current_value

I found this question extremely helpful: Python list of dictionaries search

Max xaM
  • 348
  • 2
  • 12