Anuradha Singh
Anuradha Singh

Reputation: 3

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"}
             ]

        }
    ]

Upvotes: 0

Views: 2766

Answers (5)

Sairam Polaki
Sairam Polaki

Reputation: 11

I hope the following code will meet your requirements

payload.individualdata[0].*todelete

Upvotes: 1

Anuradha Singh
Anuradha Singh

Reputation: 3

The above is now working I had to change the output to application/java and it worked. Thanks all for the help.

Upvotes: 0

Deep
Deep

Reputation: 71

same like xpath3(), MEL have JsonPath to access values from json string. Syntax:

[json:yourpath/node]

Upvotes: 0

Vij
Vij

Reputation: 445

i created methods for and save json file in asset folder try this.

     public static String loadJSONFromAsset(Context context,String 
  filename) {
    String json = null;
    try {
        InputStream is = context.getAssets().open(filename);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}

and then

 public void loadJson(){

    try {
        JSONArray mainResposeArray =  new JSONArray(Utilities.loadJSONFromAsset(Activity.this,"demo.json"));

        JSONObject mJsonObject  = mainResposeArray.getJSONObject(0);

        JSONArray itemArray = mJsonObject.getJSONArray("individualdata");


        if(itemArray.length() > 0){

            for(int pos = 0; pos<itemArray.length();pos++){
                JSONObject mSubDataObject = itemArray.getJSONObject(pos);

                String intexchangegroupname = mSubDataObject.getString("intexchangegroupname");
                String todelete = mSubDataObject.getString("todelete");

                Log.d("value",intexchangegroupname);
            }


        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

Upvotes: 0

Yevgeniy
Yevgeniy

Reputation: 2694

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.

Upvotes: 1

Related Questions