Optional Chaining in Power Automate: Prevent Errors accessing nested items in a JSON return


Optional chaining in Power Automate allows you to safely access properties and array elements without causing errors if a value is missing or doesn’t exist. If a property or index does not exist, the expression simply returns null instead of failing the entire flow.

I design the example bellow demonstrating that, it’s quiet a self-explanatory example:

Example Actions with Expressions with and without optional chaining

Flow run return

How Optional Chaining Works

Accessing a Missing Property

Without Optional Chaining (Risk of Failure)

body('Get_Row')['nonexistent_property']

  • If nonexistent_property does not exist, Power Automate throws an error and stops execution.

With Optional Chaining (Safe Approach)

body('Get_Row')?['nonexistent_property']

  • If nonexistent_property is missing, this expression returns null instead of failing.

Accessing an Array Element That Might Not Exist

Without Optional Chaining (Risk of Failure)

body('Get_Items')['value'][0]['name']

  • If the value array is empty, accessing [0] causes an error.

With Optional Chaining (Safe Approach)

body('Get_Items')?['value']?[0]?['name']

  • If the array is empty or the property is missing, this safely returns null.

Best Practices for Using Optional Chaining

1️⃣ Use ? for Any Potentially Missing Properties

When working with APIs, dynamic content, or external sources, always use ?[] to prevent unexpected failures. This is very handy in cases that the JSON return is dynamic, i.e:. if a field doesn’t have value in SharePoint the triggerOutput don’t show that field in the body.

2️⃣ Combine With if conditions to Handle Null Values

if(empty(body('Get_Items')?['value']), 'No items found', 'Processing items')

  • This checks if the value array exists before processing.

3️⃣ Using coalesce

coalesce(body('Get_Row')?['customerName'], 'Unknown')

  • If customerName is null, this returns 'Unknown' instead of null.

Conclusion

Optional chaining is an essential feature in Power Automate that enhances flow reliability by preventing failures caused by missing or null values. By applying optional chaining correctly, you can build resilient, error-proof automation workflows that handle dynamic data with confidence.

Start using optional chaining in your flows today and experience the difference in stability and robustness!

🌎 Global concepts

Some of the concepts discussed in that Article:

  • Optional chaining
  • Data Structures
    • Arrays of objects
    • JSON objects
  • JSON handling

🤖 Prompts used in this article

Language translation helper:

  • Review my post pointing duplicated words in same row.
  • Can you check the grammar and spelling also concordance sentences?
  • Add some emojis

jean.dosher@gmail.com Avatar

Leave a Reply

Your email address will not be published. Required fields are marked *