David Fumo
David Fumo

Reputation: 29

How to convert xml body to json to use getPayloadJSON() in script mediator wso2?

I developed a resource and after the payload I set messageType to application/json and I got this output when doing rest api requests:

{
  "results": [
    null,
    null,
    null,
    null,
    {
      "remainingValue": {
        "amount": 0,
        "units": "MB"
      },
      "status": "active",
    },
    {
      "remainingValue": {
        "amount": 235862,
        "units": "MT"
      },
      "status": "active",
    },
    null,
    null,
    {
      "remainingValue": {
        "amount": 0,
        "units": "MB"
      },
      "status": "active",
    },
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    {
      "remainingValue": {
        "amount": 0,
        "units": "MB"
      },
      "status": "active",
    },
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null
  ]
}

I wanted to use script mediator in JavaScript to remove the null values, but it turns out that the message in wso2 context is in soap (as shown by log mediator) and inside the script the method mc.getPayloadJSON() doesn't get that payload (because it is xml).

Any help on how I can convert the soap message context to json or another way to handle this null objects?

Upvotes: 0

Views: 196

Answers (1)

chashikajw
chashikajw

Reputation: 828

In REST API request you are getting a result as JSON which you mentioned above, You will be able to get the same results in the script mediator. mc.getPayloadJSON() should return the same JSON response if you write the script mediator correctly

Upvotes: 0

Related Questions