cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
AIUYM19
Helper V
Helper V

How to perform on-the-fly JSON queries

I have a flow which assigns values in a Select action, and some of these values need to be referenced from a JSON object. For example, suppose I need to look up the value corresponding to ID="1" and Month="2021-01" from the JSON object below.

[
  {
    "ID": "1",
    "Months": [
      {"Month": "2021-01", "Value": 123},
      {"Month": "2021-02", "Value": 456}
    ]
  },
  {
    "ID": "2",
    "Months": [
      {"Month": "2021-01", "Value": 654},
      {"Month": "2021-02", "Value": 321}
    ]
  }
]

 How would I do this? The following expression comes to mind.

@body('where_the_JSON_object_lives')?['Months']?['Value']

But how do I specify which ID and Month to look at? Since a Select action does not live inside an Apply to Each loop, it's looking at all the values at once. Also, there are thousands of objects being handled by the Select action, so using an Apply to Each with filters would take a very long time.

Ideas?

5 REPLIES 5
v-yiwenxie-msft
Microsoft
Microsoft

Hi @AIUYM19 

 

You can try use ‘Compose’ to assign values instead of ‘Select’.

 

The following flow is for your reference:

3.3-2.PNG

The dynamic content ‘Outputs’ is from ‘Compose’.

 

The expression used in ‘Apply to each 2’ is:

items('Apply_to_each')?['Months']

 

The expression used in ‘Compose 2’ is:

items('Apply_to_each_2')?['Month']

items('Apply_to_each_2')?['Value']

 

The inputs in ‘Compose 2’ is:

{

"Month":"@{items('Apply_to_each_2')?['Month']}",

"Value":"@{items('Apply_to_each_2')?['Value']}"

}

 

The running result of the flow is:

3.3-3.PNG3.3-4.PNG3.3-5.PNG3.3-6.PNG

 

If you still have the problem, please feel free to let me know.

 

Best Regards,
Community Support Team _ Kira Xie
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi, @v-yiwenxie-msft .

I'm not sure I follow... I have two JSON objects: one returned by a List rows present in a table action (let's call this Array A) and one returned by a Get file content action (let's call this Array B). The JSON array in my example above is Array B, and it is the array from which I need to look up the values. The JSON array to which I am applying the Select action is Array A, and it needs values from Array B.

Array A has thousands of objects in it, and it would be impractical to use an Apply to each action to loop through all of them to perform the lookup from Array B. It would simply take too long. I was hoping there was an expression I could put in the Select action's Value field that would perform the lookup from Array B all at once.

For example, my Select action would include the following key-value pairs...

  • Key: Month, Value: 2020-01 (from Array A)
  • Key: ID, Value: 2 (from Array A)
  • Key: Value, Value: 654 (this is the lookup I want to do from Array B)

My goal is to get this information into a single array quickly, so I do not need to perform time-consuming Apply to each and Filter actions later in the flow. By comparison, the Select action is very fast; I just don't know how to perform lookups from the Value field of that action.

v-yiwenxie-msft
Microsoft
Microsoft

Hi @AIUYM19 

 

Could you show me the detailed result you want to get from 'Select'?

And could you take a screenshot of your detailed flow with private info covered up?

Thank you for your cooperation.

 

Best Regards,
Community Support Team _ Kira Xie
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

@v-yiwenxie-msft, here are screenshots from the relevant part of the flow...

Screenshot 2021-03-10 120019.png

 

Screenshot 2021-03-10 120228.png

 

Screenshot 2021-03-10 120306.png

 

Screenshot 2021-03-10 120343.png

 

I want to add another key-value pair to that select action, such that the key would be "MarketCategory" and the value would be looked up from the Parse market categories JSON action near the top using a substring from the "Account" value.

The two issues I'm running into here are these...

  1. The "Account" value is taken from a column in the Excel table whose name can't be used in an expression, because of the presence of strange characters (Windows won't let me take a screenshot of the tooltip showing the full column name; the tooltip disappears).
  2. Even if I was able to extract the substring from the account value, I don't know how to use an expression in the Select action to look up the substring from the market categories JSON.

Here is the form of the market categories JSON...

{
    "Companies": [
        {
            "Market": "Market A",
            "Code": "157",
            "Category": "Category A"
        },
        {
            "Market": "Market B",
            "Code": "140",
            "Category": "Category A"
        },
        {
            "Market": "Market C",
            "Code": "607",
            "Category": "Category B"
        },...
    ]
}

Suppose the account substring corresponding to the "Code" is "140." Then, I want to write an expression which looks up Code=140 from the above JSON, and then returns Category="Category A." This returned value would be used as the value for the key "MarketCategory."

 

This can be done using an Apply to each action, but that would take a very long time, given the number of items extracted from the Excel table.

v-yiwenxie-msft
Microsoft
Microsoft

Hi @AIUYM19 

 

I’m afraid currently, Power Automate doesn’t support looking up specific value for a property which is not in one array except using ‘Apply to each’.

But there is a workaround for your problem if I’ve not misunderstood your need.

In my understanding, you want the flow to look up a specific value for the property ‘Code’ and return its value for the property ‘Category’.

 

Then the following steps are for your reference:

In my flow, I want to return the value of ‘Category’ for the value of ‘Code’ which is ‘140’.

And I’ve run the test successfully.

3.11-9.PNG

 

The ‘Compose’ is:

3.11-10.PNG

 

The functions used in all these ‘Composes’ are:

string(outputs('Compose')['Companies'])

indexOf(outputs('Compose_2'),'140')

substring(outputs('Compose_2'),outputs('Compose_3'))

replace(substring(outputs('Compose_4'),indexOf(outputs('Compose_4'),':"')),':"','')

substring(outputs('Compose_5'),0,indexOf(outputs('Compose_5'),'"'))

 

For more info about the functions used above:

string()  indexof()  substring()  replace().

 

Best Regards,
Community Support Team _ Kira Xie
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  

Helpful resources

Announcements
MPA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

V3_PVA CAmpaign Carousel.png

Community Challenge - Giveaways!

Participate in the Power Virtual Agents Community Challenge

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

R2 (Green) 768 x 460px.png

Microsoft Dynamics 365 & Power Platform User Professionals

DynamicsCon is a FREE, 4 half-day virtual learning experience for 11,000+ Microsoft Business Application users and professionals.

Top Solution Authors
Users online (1,942)