0
votes

Mule Transform Message:

%dw 1.0 
%function removeEmptyInArray(arr) arr map (
    (removeEmptyInArray($) when $ is :array 
    otherwise (removeEmptyInObject($) when $ is :object
    otherwise $ when ($ != null and (sizeOf $) > 0) otherwise null))
) when arr != [] 
otherwise null
%function removeEmptyInObject(obj) obj mapObject (
    '$$': (removeEmptyInArray($) when $ is :array 
    otherwise (removeEmptyInObject($) when $ is :object
    otherwise $ when ($ != null and (sizeOf $) > 0) otherwise null))
)

%output application/json skipNullOn="everywhere"  , encoding='UTF-8'
%namespace ns0 http://xxxxx
%namespace ns1 http://xxxxx

%function getKey(key) (capitalize key) replace /\s/ with '' replace /Tns:/ with '' replace /tns:/ with ''  
%function convert(obj) obj mapObject {
     (getKey($$)) : convert($) when ($ is :object) otherwise $
} 

---
{
    "Header": {
        "H1": {
            "ID": sessionVars.ID ,
                "UID": sessionVars.UID

        }
    },
       "Body": removeEmptyInObject(convert(payload))

}

Responses:

First Response:

"Details": [{
                        "A": {
                            "A1": {
                                "A11": ""
                            },
                            "A2": {

                            }
                            "A3": ""

                        }
                    },
                    {
                        "B": {
                            "B1": {
                                "B11": ""
                            },
                            "B2": ""

                        }
                    }]

Second Response:

"Details": {
                    "A": {
                        "A1": {
                            "A11": ""
                        },
                        "A2": ""
                    }
                }

The two response comes for different persons so the response can very based on ID.

But i need that every time the response should come as an object not like Array [] and i also can not hard coded the whole payload as payload can be changed as per the request.

Expected Response for first type of request:

"Details": "A": { "A1": { "A11": "" }, "A2": {

                        }
                        "A3": ""

                    }
                },
                {
                    "B": {
                        "B1": {
                            "B11": ""
                        },
                        "B2": ""

                    }
                }
1
Hi Ryan, it's a bit hard to understand what you're trying to achieve here. Can you provide an example input for each of the outputs you've provided? Also, it's difficult to tell the relationship between your mapping and output. None of the keys in your mapping correspond to your output examples, and your output examples are not valid JSON. Can you please address the above issues? Thanks.jerney
@jerney input request is just uid number. Yes the provided repose is not full json i just extracted some part of it i will explain the issue for you below. For uid : 12345 request The request is coming like this: "Details":{} But for the second request uid:987654 The response is showing like this: "Details":[{},{}] Now i want to remove the [] from the second response basically to make the response as an objects not as an array.Chris Ryan
How do you plan to contain the two objects without an array?jerney
Can be created as a separte objects?Chris Ryan
Your expected response doesn't seem to be valid JSON. Comma separated objects are for arrays. Please review what should be the expected response in valid JSON.aled

1 Answers

0
votes

If you're just looking to take:

{
  "Details": [
    {
      "A1": {}
    },
    {
      "A2": {}
    }
  ]
}

And turn it into:

{
  "Details": {
    "A1": {},
    "A2": {}
  }
}

In other words "reduce" an array to an object: you can do that with reduce:

payload.Details reduce ((detail, obj={}) ->
  obj ++ detail
)

Or you can use the shorthand:

payload.Details reduce $$ ++ $

This is explained in detail here.