'Power Automate parse JSON with dynamic property name

I am working on a flow to gather information from API and load it to the database.

However, while getting the response from the API call, one of the property names is variable per account.

Then the schema I am getting from the API call is like this:

{
  "type": "object",
  "properties": {
    "12305": {
      "type": "array",
      "items": {
        "type": "integer"
      }
    }
  }
}

The property "12305" from the response varies from account to account. And I need to go through all accounts in one run.

Unfortunately, it is not possible to add variable content to the Schema value of the Parse JSON action.

Does anyone know the solution for that, please?



Solution 1:[1]

so the solution for such case is following.

  1. The JSON response needs to be converted to the string.
  2. In the string the variable account id needs to be replaced to a static value
  3. From the output new json needs to be created
  4. Done.

Here are the steps printscreened: final flow

Solution 2:[2]

When you call this API, do you have the Account number available? or how are you querying the information to this API?

Option 1:

Assuming you get an account number or an array of account numbers that you have to process, you can use that schema and just set the variable for the account number to get the response.

This is an example with one account number, if you have an array you'll just have to create an "Apply to each" action

Flow example

Here is the flow run for this method

The variable: Variable AcountNumber

Flow run

Option 2

If you do not have the account number, and are using other data to query this account's information, then it gets interesting, and you have to parse the response from the API as a string. Fortunately this is a simple schema so it will be something like this:

After we get the response from the API, the action "Get Account Number from API's JSON response 1" will find the first double quote character (since we know there will only be one property according to the schema) that will give us the starting index of the account number. Then, on action "Set variable" we search for the second double quote character (closing string) and now we have the account number from the response. After that, we parse the response using the schema you already have and then get the array of integers using the variable we set.

Option 2 flow

Here is the flow run for this method

Parse JSON response string

option 2 run result

Expressions shown in the examples (Please note that if you change the actions names you'll have to update this expressions so that it can match your actions. Also I am not validating the response from the API)

  • body('Parse_JSON')?[variables('AccountNumber')]
  • substring(outputs('JSON_response'),add(indexOf(outputs('JSON_response'),'"'),1))
  • substring(outputs('Get_Account_Number_from_API''s_JSON_response_1'),0,indexOf(outputs('Get_Account_Number_from_API''s_JSON_response_1'),'"'))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Petr Hellmich
Solution 2 Luis Carlos Arellano Ochoa