Could someone explain why some expressions contain a question mark in them while others not?
What is the difference between the two below?(Please note the '?' in the second one)
Solved! Go to Solution.
The use of the question mark in JSON specifies that if the field being referenced doesn't exist then a NULL value will be returned instead of throwing an error. One of the common spots where you won't see the ? is when you are referencing a value that is part of an array. For example
item()?['some field']['1']?['value field']
In this example 'some field' may or may not be returned as part of a JSON object. If its not returned then a null will be returned if you try to use the value of the field. But the field is supposed to be part of an array and we want the second element in the array. In the case of the index the value either exists or its an invalid array index, so we return an error rather than a null.
The use of the question mark in JSON specifies that if the field being referenced doesn't exist then a NULL value will be returned instead of throwing an error. One of the common spots where you won't see the ? is when you are referencing a value that is part of an array. For example
item()?['some field']['1']?['value field']
In this example 'some field' may or may not be returned as part of a JSON object. If its not returned then a null will be returned if you try to use the value of the field. But the field is supposed to be part of an array and we want the second element in the array. In the case of the index the value either exists or its an invalid array index, so we return an error rather than a null.
Thanks a lot!! That clarifies my doubt.
The first part of that made sense and then it was just words. I understand the use of question marks sometimes (optional parameters), but I am still not quite understanding it's use in an array, as I do not entirely understand JSON. Can anyone expand?
The ['1'] in the example I gave is the integer index to the array item. In regular programming languages it is common to reference the entries in an array using an integer index, like A(0), A(1), A(2), etc. This is just the JSON version of that encoding.
This is so confusing. Is the structure of the language documented anywhere? I am an experienced programmer, but it is very difficult to write code by guessing what the syntax is. I don't even understand what item() means or why you need single quotes to get the first item in an array.
The syntax is just JSON Object notation. You can find a variety of web resources that teach JSON.
Item() in this case is the current item in an Apply to each loop. So Item()['Title'] means the Title property of the current item in the loop. When you see the question mark between them it means the following property is optional and may not exist. If it doesn't return a null. So
item()?['some field']['1']?['value field']
is
item() - the current item in the loop
?['some field'] - an optional property of the item. In this case its an array
['1'] - the first array item of that optional property
?['Value field'] - a property of the first array item
JSON doesn't have parentheses or question marks. I think it might be Javascript. @Pstork1
I'll have to figure out how to write it in order to use Power Automate. Javascript was not required to write the custom connector.
JSON is "JavaScript Object Notation", so yes it is Javascript. JSON itself doesn't make use of parentheses or ?, but the syntax for identifying the path to JSON objects, arrays, and properties does.
This blog post may help as an introduction to the notation. How to get a specific value from a JSON in Power Automate (tomriha.com)
Why is this happening?
I'm not positive without seeing more. But my suspicion would be that you entered the JSON reference directly in the value field instead of the Expressions Tab on the dynamic content dialog. The syntax you are using is to JSON like XPath is to XML. It identifies the location of a value. You entered it as a string when it needs to be entered the same way you would enter a formula. This is from one of my flows. What I'm entering is the JSON reference for the PA dynamic content. If you do a Peek code you'll see the same syntax surrounded by @{...}. Most of the time you can just use the dynamic content dialog. But sometimes you have to hand enter the JSON to get exactly what you want.