0

I have created an empty list and appending to it in the flow, at the end, I need to get the value of a specific key through an Expression, Is this possible? and if yes, how?

Please see the below screenshots.

Initialise:

enter image description here

Append:

enter image description here

I am appending data to this array in a Do while loop, the content of each run (total 2 run) is shown below:

First Run:

enter image description here

Second Run:

enter image description here

And the output of Compose"

[
  {
    "name": "Fy2020",
    "value": 2.5
  },
  {
    "name": "Fy2021",
    "value": 2.5
  } 
]

Retrieve and assign it to another variable:

enter image description here

I have tried different approaches and got different errors:

variables('FYearsValues')?['value']?[0] erorr:

 'The template language expression 'variables('FYearsValues')?['value']?[0]' cannot be evaluated 
     because property 'value' cannot be selected. Array elements can only be selected using an 
   integer index. Please see https://aka.ms/logicexpressions for usage details.'.

variables('FYearsValues')?['Value']?[0] Error:

The template language expression 'variables('FYearsValues')?['Value']?[0]' cannot be
  evaluated because property 'Value' cannot be selected. Array elements can only be selected using an
  integer index. Please see https://aka.ms/logicexpressions for usage details.'.

variables('FYearsValues')[0] Error:

The variable 'FYCounter_' of type 'Integer' cannot be initialized or updated with value '{
 "name": "Fy2020",
 "value": 2.5
 }' of type 'Object'. The variable 'FYCounter_' only supports values of types 'Integer'.

and for this: variables('FYearsValues')['Fy2020'] Error:

Unable to process template language expressions in action 'Set_variable_3' inputs at line '1' and 
column '2857': 'The template language expression 'variables('FYearsValues')['Fy2020']' cannot be 
 evaluated because property 'Fy2020' cannot be selected. Array elements can only be selected using an 
integer index. Please see https://aka.ms/logicexpressions for usage details.'.  

The best formula for me would be something like this

variables('FYearsValues')['Fy2020'] which needs to return 2.5 (i.e.value)

but I can work with indexes too (e.g. variables('FYearsValues')[0] which needs to return 2.5 (i.e.value)

1
  • I can also work with any similar object like a data dictionary or something else, I mainly need to be able to access the value by calling the key, maybe a string that I can split with ',' and then do another split based on ':' and this must be done through Expression Sep 18 '20 at 5:36
1

If it's an array, you would access it with index starting at 0 like

 variables('MyArray')?[1]

Index can be a variable as well, in that case it would be like

variables('MyArray')?[variables('varIndex')]

enter image description here

However, if it's a JSON object collection, then first need to find the item by the index and then find the items' value by property name. In the example below, [results] is a collection of items, but retrieving user email by property

body('Parse_JSON')?['d']?['results'][1]['User']['Email']

9
  • Thanks there, Can you please explain the JSON part more, what is json format and what are User and Email that you are referring to? Regarding "then first need to find the item by the index" How can I find the correct index? Sep 18 '20 at 22:25
  • Added more screenshots to show the content of my JSON array? Do I need to Compose? Sep 18 '20 at 22:38
  • Upvoted (and Thx again) as after composing and using outputs('Compose')?[0]?['name'] and outputs('Compose')?[0]?['value'] I was able to get the name and value of array but still need to find the value based on a given name in the Expression, (i.e. not through the apply each) . However, I am able to find the correct index by using some variables dynamically but still keen to see if there is a quick way to search an array based on name in an Expression Sep 18 '20 at 23:12
  • 1
    To check if a value exists in an Array, a collection of JSON objects or a string variable, use contains() function like: contains(variables('MainArray'), 'Matt'). It returns true or false. Let me know if you have any other questions Sep 19 '20 at 2:34
  • Thx @Matiur, I am running contains(outputs('Compose'), 'Fy2020') as an expression and it always returns false while this item as 'name' exists in my JSON array, do you know how could I change the query to return true? Sep 21 '20 at 20:13
2

I have created an empty list and appending to it in the flow, at the end, I need to get the value of a specific key through an Expression, Is this possible? and if yes, how?

Initialize Variables:

Your example array

example array

Integer counter (used in do until loop)

temporary string variable

Temporary String tempString (used to get value from array)

current string value from array

Loop through Array:

high-level array loop

Do Until Loop Details

do until loop condition details

Switch Loop Details

switch loop condition details

variables('FYearsValues')[variables('counter')].name

Switch Loop Condition Case Details

switch loop condition case details

variables('FYearsValues')[variables('counter')].value

Increment Variable Details

increment counter in do until loop condition

1
  • Thanks for the reply, although the problem was solved a long time ago, upvoted. Sep 12 '21 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.