All documentation

How to Convert a Parsed JSON Object to an Array in Power Automate

Published January 20, 2024 Guide

Introduction:

In Power Automate, working with JSON data is a common task. Often, we parse JSON data and need to manipulate it for further actions. A frequent challenge arises when a parsed JSON object needs to be converted into an array for subsequent steps in the flow. This article will guide you through the process of converting a single parsed JSON object into an array using Power Automate.

Scenario:

Imagine you have a JSON object returned from an API call or another data source. You've successfully parsed this object using Power Automate's "Parse JSON" action, but your next step in the flow requires an array, not a single object. We'll show you how to address this scenario.

Step-by-Step Guide:

  1. Parse Your JSON Data: Begin by using the "Parse JSON" action to interpret your JSON data. This action converts your JSON string into a Power Automate object.
  • Add a "Parse JSON" action.
  • Provide your JSON content in the "Content" field.
  • Define your schema in the "Schema" field.
  1. Compose an Array: After parsing, use a "Compose" action to transform the JSON object into an array.
  • Add a "Compose" action immediately after your "Parse JSON" action.
  • In the "Inputs" field of the "Compose" action, use the following expression to create an array: createArray(body('Parse_JSON')?['eventdata']) Note: Replace 'Parse_JSON' with the name of your "Parse JSON" action. Adjust 'eventdata' to the specific property you are targeting.
  1. Use the Composed Array: The output of the "Compose" action is now an array containing your original JSON object. You can use this array in subsequent actions that require an array input.
  • Reference the output of the "Compose" action in any subsequent step that requires an array. For example, in a "Select" action, use the output of the "Compose" action in the "From" field.

Conclusion:

This method effectively wraps your single JSON object in an array, making it compatible with Power Automate actions that require array inputs. It’s a simple yet powerful technique that can be particularly useful when dealing with APIs or data sources that return single JSON objects.

Best Practices:

  • Always verify the structure of your JSON data before parsing.
  • Use the "Compose" action to transform data types and structures.
  • Test your flow with different input data to ensure it handles various scenarios gracefully.

By following these steps, you can seamlessly convert a parsed JSON object into an array in Power Automate, enhancing the versatility and functionality of your automated workflows.