cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
saleelpk95
New Member

Use of Question Mark(?) in expressions

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)

item()['some field']
item()?['some field']
1 ACCEPTED SOLUTION

Accepted Solutions
Pstork1
Dual Super User
Dual Super User

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.

 



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

View solution in original post

11 REPLIES 11
Pstork1
Dual Super User
Dual Super User

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.

 



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

Thanks a lot!! That clarifies my doubt.

ANCC
Frequent Visitor

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?

Pstork1
Dual Super User
Dual Super User

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.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
Harold_Anderson
Helper II
Helper II

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 



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
Harold_Anderson
Helper II
Helper II

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.

Pstork1
Dual Super User
Dual Super User

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.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

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)



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
Harold_Anderson
Helper II
Helper II

Why is this happening?

Harold_Anderson_0-1645037140608.png

 

Pstork1
Dual Super User
Dual Super User

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.

image.png

 



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

Helpful resources

Announcements
MPA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

Power automate tips 768x460 v2.png

Restore a Deleted Flow

Did you know that you could restore a deleted flow? Check out this helpful article.

Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Users online (2,386)