Nav

To Drill Down into JSON (Design Center)

This procedure describes how to use For Each to access nested JSON elements in an object. For Each iterates over and processes each element separately. For example purposes, each time the process repeats, you log the value of the element and the counter. In a real-world situation, instead of a simple log operation, you might insert each element in a database for analysis or perform other operations.

To configure For Each, you use dot notation in the expression to specify the path to the JSON element of interest. For example, to apply some processing to bs in the company object, you set the Collection field to payload.*company.bs. The asterisk prefix accesses all members of company.


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
     ...
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  },
  ...
  }
]

In the configuration, you use a default batch size of one because the example data you use consists of only 10 objects. If you have 200 objects and a batch size of 50, the app iterates over 50 objects at a time, returning four separate results, one for each batch.

This procedure builds on the procedures to create a basic Mule app and to add an expression to make dynamic requests. After performing these procedures, you have HTTP listener and HTTP request in the Consume API flow, and you can perform the following steps to add For Each to the flow.

  1. Proceed to the next step if the app is running. Otherwise, click Deploy. Execute the flow as follows:

    • Click Copy Link.

    • Paste the link in a browser or client, and type /getpath?path=users at the end.

      The HTTP Listener hears the request, and starts the app. The JSON Placeholder profiles of all users appear in the browser.

  2. In Design Center, add and configure For Each:

    • Click add For Each after HTTP Request.

    • Select For Each.

    • Configure For Each as follows:

      • In Collection, type the expression, using dot expression, to access the list, array, or map you want to iterate over . For example, type the following expression to access the bs element in company: payload.*company.bs

      • In Batch Size, accept the default 1.

      • In Root Message Variable Name, accept the default.

      • In Counter Variable Name, change the default to drillcounter just for example purposes.

        foreach configuration
    • Close For Each.

  3. Within For Each, click add Logger and select Logger. Configure Logger as follows:

    • In Message, click graphically construct mapping design center task 8a822.

    • Type the following expression:

      payload " count is: " vars.drillcounter

    • Close the Logger.

  4. To the right of and outside of For Each, add another Logger. Configure Logger as follows:

    • In Message, click graphically construct mapping design center task 8a822.

    • Type the following expression:

      payload

  5. Click Deploy, and run the app as described in step 1. Check the logs to see 10 messages, one for each iteration followed by the last message that contains the original input to the flow.

    INFO15:34:35harness real-time e-markets count is: 1
    INFO15:34:35synergize scalable supply-chains count is: 2
    INFO15:34:35e-enable strategic applications count is: 3
    INFO15:34:35transition cutting-edge web services count is: 4
    INFO15:34:35revolutionize end-to-end systems count is: 5
    INFO15:34:35e-enable innovative applications count is: 6
    INFO15:34:35generate enterprise e-tailers count is: 7
    INFO15:34:35e-enable extensible e-tailers count is: 8
    INFO15:34:35aggregate real-time technologies count is: 9
    INFO15:34:35target end-to-end models count is: 10
    INFO15:34:35[{id=1, name=Leanne Graham, username=Bret, email=Sincere@april.biz, address={street=Kulas Light, ...

In this topic: