Search code examples
mulemule-studiomule-el

How to extract nested json elements in mule flow without changing it to Object?


I have a requirement wherein I need to extract the value of a key from JSON input into the mule flow to check condition in choice flow control. My JSON is something like this and I want to extract "todelete". I do not want to change my JSON to Object. Please suggest.

    [
        {
            "selectiontypeid" : 133,
            "internalrecipientstatusid" : 123,
            "createdby" : "Anu",
            "createddate" : "06/26/2017",
            "authorizedby" : "Anu",
            "authorizeddate" : "06/26/2017",
            "individualdata" :

             [
                        { "intexchangegroupname" : "abc", "employeename" : "abc", "todelete" : "Y"},
                        { "intexchangegroupname" : "abc", "employeename" : "abc", "todelete" : "N"},
                        { "intexchangegroupname" : "abc", "employeename" : "abc", "todelete" : "N"}
             ]

        }
    ]

Solution

  • todelete alone does not make any sense to me. you probably want the individualdata array, right?

    just use the following MEL #[payload.individualdata]

    or the following dataweave:

    %dw 1.0
    %output application/json
    ---
    payload.individualdata
    

    which will result in following json:

    [
      [
        {
          "intexchangegroupname": "abc",
          "employeename": "abc",
          "todelete": "Y"
        },
        {
          "intexchangegroupname": "abc",
          "employeename": "abc",
          "todelete": "N"
        },
        {
          "intexchangegroupname": "abc",
          "employeename": "abc",
          "todelete": "N"
        }
      ]
    ]
    

    if you would like to get rid of the outer array, apply flatten on payload.individualdata in the dataweave.