Tuesday, September 26, 2023

How to loop JSON through an array in a logic app?

 Hi guys, 

I have a requirement to update multiple records in F&O and send a response for each record using a logic app.


Please find the request and response in the below screenshot.



To archive my process I have followed the below steps.


1. I have utilized the 'When a HTTP request is received' trigger.



2. Initialized the 2 variables(Array, string type).


3. I have used a 'for-each' loop to iterate through each record, passing the HTTP response body as input. Inside the loop, I have parsed the current record.

4. After parsing, I updated the record in F&O using the 'update record' trigger.

5. If an update is successful, I am assigning a success status to the 'message' variable.
    If the update fails, I am parsing the error and sending it to the 'message' variable.

  •     While parsing the error I have used the below JSON.
    {
	"status": 400,
	"message": "An error has occurred.  Write failed for table row of type 'CustCustomerV3Entity'. Infolog: Warning: ",
	"error": {
		"message": "An error has occurred.  Write failed for table row of type 'CustCustomerV3Entity'. Infolog: Warning:"
	"source": "URL"
    }
    To fetch the above error use the below expression or parse JSON.
    outputs('Update_record')?['error']?['code'] 
    Status code:
    outputs('Update_record')?['statusCode']
  •  Afterward, I use 'Append to array variable' to create the response JSON and store it in the array.

6. Configure run after options for Append to the array variable has set up in the below.

7. After the for loop, I used the 'HTTP Response' action. In the response body, I converted the array into a JSON string.
json(string(variables('OutputJson')))

8. Open the 'For each' setting and setup like the below screenshot.



The output you will find in 1st screenshot.


Keep Daxing!!

No comments:

Post a Comment