Click here to Skip to main content
15,860,861 members
Articles / Hosted Services / Azure

Azure Logic Apps - Debatching Pattern

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
19 Dec 2017CPOL4 min read 10.6K   1  
This article explain in brief about using ForEach loop in Azure Logic app to debatch the JSON File

Introduction

This will help as quick start hands-on for those seeking debatching pattern on Azure logic apps. Though there could be other ways to split the file like using JSON "SplitOn" in the request, but I am using For Each loop to debatch it.

Background

On premise, BizTalk developer or citizen integration developer who has no prior knowledge of BizTalk can start with Azure Logic app development. You must need Azure subscription to develop this and SOAP UI or Postman tool to test the solution.

Using the Code

Scenario

To split the batch order file into individual order and send it to destination path.

For this scenario, I will use batch order file JSON formatted and destination path to Google Drive to send the debatched orders.

The batched JSON file sample is below. It has four orders in the batch. The expected output is four files to be written with their details into Google drive account folder.

JavaScript
{
  "OrderDetails":{
  "BatchNo": "ORDERS078",
  "Orders":[
    {"Order":{"Id": "100","ProductName": 
    "Laptop","ProductCategory": "Electronics",
    "Productmanufacturer": "DELL",
    "Qty": "1","Price": "700"}},
    {"Order":{"Id": "200","ProductName": 
    "Mobile","ProductCategory": "Electronics",
    "Productmanufacturer": "Apple","Qty": 
    "1","Price": "499"}},
    {"Order":{"Id": "300","ProductName": 
    "TV","ProductCategory": "Appliance",
    "Productmanufacturer": "SONY","Qty": 
    "2","Price": "950"}},
    {"Order":{"Id": "400","ProductName": 
    "Swimming Kit","ProductCategory": "Sports",
    "Productmanufacturer": "Golddust","Qty": 
    "1","Price": "450"}}
    ]
  }
}

Steps to Develop the Logic Flow

I presume that you have Azure subscription and you understand how to navigate to create Logic App workflow.

  1. Create Logic Apps with Name. Select the subscription and you can either create new resource group or use an existing one, select the location and click on create button. It takes few moments to create in Azure.

    Image 1

  2. Go to Logic App dashboard and select the above logic app created in step 1. This will prompt a page to design the flow where you have the option to select “Blank Logic App” from template. On the designer, select HTTP connector with Trigger "When HTTP Request received". Click on the option "Use Sample Payload to Generate Schema". In the dialog box, copy the sample provided in the scenario section. Once click done, this will generate the JSON schema.

    Image 2

  3. Click on next Step below the first flow and go to option (More...) section to add For Each loop, then select the Order from dynamic content. This appears because the Schema structure is already defined.

    Image 3

  4. Click on the "Add Action" inside the For Each loop and select the Google drive Action for Creating a file. You can type search to find in the search option given. Once you select this option, it will ask you to provide the Google account details to authorize the logic apps to write files in your Google drive. You can create a separate folder in Google drive for this application to test your files.

    Select the folder path by navigating to the folder picker icon and File Name = Set date time

    To do this, click on File Name type box and it will pop up for Dynamic content and Expression. Select Expression and scroll down for Date time function to select utcNow(). I have chosen this to keep each file different name with date time. There are various other functions you can explore to set specific File name with macros.

    File Content value will be set in the next step.

    Image 4

  5. Go to code view mode of logic App designer to set the File Content value. You would see body value is empty at line # 18. See the highlighted screen for this.

    Type the content for value --- ”@items('For_each')?['Order']".

    Line #18 will look like ”body": "@items('For_each')?['Order']",

    Image 5

  6. Go back to designer mode and expand the first shape HTTP Receive to set the Advance Option Method =POST and Copy the HTTP Post URL and save the project.

    Image 6

  7. Test it using any test tool like SOAP UI or Google Postman. I am using Postman to test it as per below screen. Use POST verb and url that you copied from Logic App designer HTTP receive first shape. Make sure you select raw body and use content type application/json. The same sample message given in scenario use for testing. After test, you would see your Google drive folder with four files written. You can view the content of the files as well.

    Image 7

Complete Developed Designer View

Image 8

At a Glance View

Image 9

Test Result

View order files in Google drive and also you can track the transaction history in Logic apps Run history. See the images below for details.

Image From Google Drive:

Image 10

Image from Logic App run history:

Image 11

You can click on the transaction details for depth analysis.

Image 12

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
12+ years in IT industry. Passionate for Microsoft new trending technology. My expertise is Microsoft Integration tool.

Comments and Discussions

 
-- There are no messages in this forum --