-1

Prepared json request like below.

[{
    "type": "John",
    "attributes": {
        "AA": [{
            "value": "1234"
        }]
    }
}, 
{

}
]

I need to replace the below one with empty i.e means blank ''.

, 
    {

    }

Could you please provide the solution for this.

Finally It should come like below.

[{
        "type": "John",
        "attributes": {
            "AA": [{
                "value": "1234"
            }]
        }
    }
]
Gopi
  • 105
  • 1
  • 7
  • 24

4 Answers4

0

This regex matches the given sequence however you would probably need to change it to accept all possibilities:

/, \n\{\W+?\}/

Just replace the match with nothing.

EDD
  • 2,070
  • 1
  • 10
  • 23
  • It is giving Illegal escape sequence.could you please provide exact one :) – Gopi Dec 26 '16 at 14:34
  • tried having payload.replace with above expression. Did not work. Could you please someone will provide solution how to do it in mule :) – Gopi Dec 26 '16 at 15:13
  • @Gopi remember that SO is not a free coding platform. Have you ever written a regex? You need to try things yourself, this is just a generic regex, it needs proper escaping to work in whatever environment you have. – EDD Dec 26 '16 at 19:31
0

Do you get the response as a JSON object or as a string?

If you get the response as an object you have to stringify it before applying the replace function:

payload = JSON.parse(JSON.stringify(payload).replace(/,\{\}/, ''))

If the response you posted above is already stringified and you haven´t parsed it into an object, the method is:

payload = payload.replace(/\,\s+\n\s+\{\n\s+\}/,'')
  • preparing the request as json and sending it over http call before sending it to http. we need to remove the whereever we have , { } and send the request. – Gopi Dec 28 '16 at 12:34
  • Then just do the first option: payload = [{ "type": "John", "attributes": { "AA": [{ "value": "1234" }] } }, { } ]; payload = JSON.parse(JSON.stringify(payload).replace(/,\{\}/, '')) where payload is your object – Jonathan Tefera Endale Dec 28 '16 at 18:20
0

To achieve this purpose, we can use DataWeave expression whether in Transform Message or in MEL.

In this case I prefer to use it in MEL: #[dw('payload filter (sizeOf $) > 0')]

sulthony h
  • 1,279
  • 1
  • 8
  • 9
  • Hi Sulthony- I have same use case as questioner. Where exactly do you use this MEL? Payload need to be json or object? Thanks! – maatthias Apr 12 '17 at 10:59
  • I will use that MEL inside the **Set Payload** transformer, and the payload can be both: json or object. – sulthony h Apr 13 '17 at 01:37
0

You can use the flatten operator here as given below. It should remove empty json. Also you can try replace {} with null and adding skipnullon="everywhere"

flatten payload
Val
  • 6,585
  • 5
  • 22
  • 52
Srinivas
  • 92
  • 3