0

I need a way to remove leading square brackets from a JSON file. I'm doing it in SAP cloud platform integration (CPI). Currently I was thinking of using groovy but can't seem to find a way to do it. Here's the JSON example:

[{
        "salesOrderNumber": "1234567",
        "orderStatus": "Complete",
        "customerPONumber": "7654321",
        "soldToID": "ABC",
        "soldToName": "CBA"

    }
]

Thank you in advance.

The code I used was for getting just one element, but I need to get multiple in case there are many.

def Message processData(Message message) {

    def body = message.getBody(String.class);
    def jsonParser = new JsonSlurper();
    def jsonObject = jsonParser.parseText(body);

    def json = JsonOutput.toJson(jsonObject[0]);

    println(json);

    message.setBody(json);
    
    return message;
}
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • 1
    Please add the code you have tried and how it failed (e.g. errors, stacktraces, logs, ...) so we can improve on it. – cfrick Jul 18 '21 at 17:42

1 Answers1

1

You have a list of objects in json. Remove square brackets means get rid of the list and keep only one element of this list.

Btw, what if there are several or zero elements in the list?

So, your algorithm

  • parse json
  • get first element list[0]
  • serialize result back into json

As a code reference look at this documentation: https://groovy-lang.org/json.html

daggett
  • 26,404
  • 3
  • 40
  • 56