Skip to main content

How to Read a Property Safely with the Question Mark Operator in Power Automate

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Reading a property that isn't there makes a flow fail, unless you put a question mark in front of it. With ?['property'], a missing property gives you nothing and the flow carries on. This guide reads a property that exists and one that doesn't, with and without the question mark.

Syntax​

General form
<object>?['<property>']
  • <object>: anything that returns an object, such as outputs('<actionName>'), body('<actionName>') or triggerBody().
  • <property>: the property's name, in single quotes.
  • ?: Microsoft's Workflow Definition Language reference calls it the null ignore operator: If the parent object that appears before the ? operator is null or is missing the referenced property, the entire expression returns null, rather than the workflow failing. When the property exists, you get its value.

Without the ?, <object>['<property>'] returns the same value when the property exists, and fails when it doesn't.

The Example​

A flow, Read a Property Safely, has four Compose actions (How to Use the Compose Action in Power Automate) after Manually trigger a flow (How to Create an Instant Cloud Flow Inside a Solution in Power Automate):

ActionInputsReads
Person{ "name": "Ada" }Nothing: it holds the object
Nameoutputs('Person')?['name']A property that exists
Age with a question markoutputs('Person')?['age']A property that's missing
Age without a question markoutputs('Person')['age']The same missing property, without ?

Step 1: Hold an Object​

Add a Compose action, rename it Person, and type this JSON in Inputs:

Person → Inputs
{
"name": "Ada"
}

The Person Compose action with a JSON object in Inputs whose name property is Ada

The object has one property, name. It has no age.

Step 2: Read the Properties​

Add the three other Compose actions with these expressions in Inputs (How to Add an Expression in Power Automate):

Name → Inputs
outputs('Person')?['name']
Age with a question mark → Inputs
outputs('Person')?['age']
Age without a question mark → Inputs
outputs('Person')['age']

Step 3: Run the Flow​

Save the flow and test it manually (How to Test a Cloud Flow Manually in Power Automate). Name returned Ada:

The run results of Name with Inputs Ada and Outputs Ada

Age with a question mark found no age, returned nothing and succeeded:

The run results of Age with a question mark with empty Inputs and empty Outputs

Age without a question mark failed, and the run failed with it:

The run with green ticks on Manually trigger a flow, Person, Name and Age with a question mark, and a red error on Age without a question mark with the InvalidTemplate message

Its Run results start with InvalidTemplate:

The run results of Age without a question mark with an InvalidTemplate error that begins Unable to process template language expressions in action 'Age_without_a_question_mark' inputs at line

This is the whole message:

The error
InvalidTemplate. Unable to process template language expressions in action 'Age_without_a_question_mark' inputs at line '0' and column '0': 'The template language expression 'outputs('Person')['age']' cannot be evaluated because property 'age' doesn't exist, available properties are 'name'. Please see https://aka.ms/logicexpressions for usage details.'.

The message names the missing property and lists the ones that exist, which helps when a name is misspelled.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out the ? on a property that may be missingThe flow fails with InvalidTemplateWrite ?['<property>']
Using ? to hide a misspelled nameThe result is always empty, with no error to tell youCheck the name against a run's output

Troubleshooting​

SymptomLikely causeFix
property '…' doesn't exist, available properties are '…'The property is missing, or its name is spelled differentlyUse a name from the list, or add ?
The result is always emptyThe name doesn't match any propertyCompare it with the object in a run
The flow fails although ? is thereAnother part of the expression failsRead the whole error message

FAQ​

Should I always write the question mark?​

When a property might be missing, yes. When a property must be there, leaving the ? out makes the flow stop at the point where something went wrong, which can be what you want.

Does the question mark work for arrays?​

Yes. Microsoft's reference says it lets you safely access a property or array element.

Why does dynamic content write ?[ for me?​

Tokens from dynamic content use the safe form too, which is why expressions built from them read ?['…'].

Conclusion​

<object>?['<property>'] returns the property's value, or nothing when the property is missing. Without the ?, a missing property fails the flow with InvalidTemplate … property '…' doesn't exist. Here outputs('Person')?['age'] returned nothing, and outputs('Person')['age'] stopped the run.

Additional Resources​