Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more
The expression language is a powerful tool within data pipelines that provide an extensive number of functions and features. When working with JSON objects such as a pipeline parameter of type object, or a pipeline activity’s output, you may come across a scenario where the field that you would like to reference may not exist, a non-existent property.
For example, I have a Lookup activity that is reading a JSON file. Proceeding the Lookup, I want to capture the field, ‘missingfield’ and set the value to a variable called v_string_file_name.
The Lookup activity returns:
{ "count": 1, "value": [ { "data": [ { "id": "123" }, { "id": "456" } ] } ] }
The expression in the Set variable activity is:
@activity('Lookup1').output.value[0].missingfield
In this case, the pipeline activity will fail, returning an error message similar to:
The expression 'string(activity('Lookup1').output.value[0].missingfield)' cannot be evaluated because property 'missingfield' doesn't exist, available properties are 'data'.
To circumvent and handle this scenarios, we can use the question mark ‘?’ operator to access the non-existent property as a null value. This operator is inserted into the expression, prior to the dot notation ‘.’ that references the desired field/property.
@activity('Lookup1').output.value[0]?.missingfield
Note: For the C# developers out there, this operator does differ from the member access and null-conditional operators and expressions, see references.
By doing so, the Set variable activity will now succeed and return:
{"name": "v_string_file_name", "value": null}
We can now wrap the Set variable expression with the coalesce function to specify a desired value when the field does not exist.
@coalesce(activity('Lookup1').output.value[0]?.missingfield, 'Property does not exist')
The Set variable activity will now return:
{"name": "v_string_file_name", "value": "Property does not exist"}
References
Expressions and functions - Microsoft Fabric | Microsoft Learn
Reference guide for expression functions - Azure Logic Apps | Microsoft Learn
Workflow Definition Language schema reference - Azure Logic Apps | Microsoft Learn
Member access and null-conditional operators and expressions: - C# reference | Microsoft Learn
Conclusion
Thank you for taking the time to read this blog. I hope you have found this useful. Please consider giving a thumbs up and leaving a comment with questions or any recommendations for future blogs.
Thank you @RDenkewalter for showing me this wonderful operator!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.