How to Check if a JSON Array Contains Objects in Logic Apps
Logic Apps are a powerful tool for automating workflows and connecting different applications. However, working with JSON data can sometimes be tricky, especially when you need to check if a JSON array contains specific objects. In this article, we will explore how to achieve this in Logic Apps using the "Parse JSON" and "Filter Array" actions.
Scenario: Imagine you have a Logic App that receives a JSON payload containing an array of user objects. You need to determine if the array contains a user with a specific email address.
Original Code:
{
"type": "Microsoft.Logic/workflows/actions",
"apiVersion": "2016-06-01",
"name": "CheckUserExists",
"inputs": {
"actions": [
{
"type": "ParseJson",
"inputs": {
"content": "@triggerBody()",
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"email": {
"type": "string"
}
}
}
}
},
"outputs": {
"body": "@body('ParseJson')",
"array": "@body('ParseJson')"
}
},
{
"type": "FilterArray",
"inputs": {
"array": "@outputs('ParseJson')['array']",
"condition": "@equals(item()['email'], '[email protected]')"
},
"outputs": {
"body": "@body('FilterArray')"
}
}
]
}
}
Understanding the Code:
- Parse JSON Action: This action takes the incoming JSON payload and parses it based on the provided schema. The schema defines the structure of the JSON array, including the types of properties in each object (e.g., integer, string).
- Filter Array Action: This action takes the parsed array and applies a condition to filter the array. The condition in this example checks if the "email" property of each object in the array matches "[email protected]".
Insights and Explanation:
- The "Parse JSON" action is crucial for ensuring the Logic App can understand the structure of the incoming JSON data.
- The "Filter Array" action is flexible and allows you to apply different conditions based on your needs. You can use other comparison operators (e.g., "greaterThan", "lessThan") or combine multiple conditions with logical operators (e.g., "and", "or").
- The "outputs" property of each action allows you to store the result of the action and use it in subsequent actions.
Additional Value:
You can extend this approach to handle different scenarios. For example, you can check if a JSON array contains objects with a specific ID, date, or any other property. You can also use nested conditions within the "Filter Array" action to filter based on multiple criteria.
References and Resources:
- Logic Apps Documentation: https://docs.microsoft.com/en-us/azure/logic-apps/
- Azure Functions Documentation: https://docs.microsoft.com/en-us/azure/azure-functions/functions-overview
- JSON Schema Validation: https://json-schema.org/
This article demonstrates how to check if a JSON array contains specific objects in Logic Apps. By understanding the "Parse JSON" and "Filter Array" actions and their capabilities, you can effectively process and filter JSON data within your Logic App workflows.