0

I have below payload and I want to convert schedule elements into array with square brackets .

{
   "value":{
      "ID":"1c1238c8-3517-47c7-83de-6269fa6098cc",
      "scheduleElements":{
         "ID":"92a1352d-8319-4e1a-b921-0d7d0ee9f59e",
         "parentItem_ID":"1c1238c8-3517-47c7-83de-6269fa6098cc",
         "startDate":"2025-05-30",
      }
   }
}

I want to convert as below payload how do I do that ?

"value":{
      "ID":"1c1238c8-3517-47c7-83de-6269fa6098cc",
      "scheduleElements":**[**{
         "ID":"92a1352d-8319-4e1a-b921-0d7d0ee9f59e",
         "parentItem_ID":"1c1238c8-3517-47c7-83de-6269fa6098cc",
         "startDate":"2025-05-30",
      }**]**
   }
}
1
  • Please add the code you have tried and how it failed (e.g. errors, stacktraces, logs, ...) so we can improve on it. Commented Apr 8, 2021 at 6:08

1 Answer 1

1

The following code:

import groovy.json.*

def data = '''
{
   "value":{
      "ID":"1c1238c8-3517-47c7-83de-6269fa6098cc",
      "scheduleElements":{
         "ID":"92a1352d-8319-4e1a-b921-0d7d0ee9f59e",
         "parentItem_ID":"1c1238c8-3517-47c7-83de-6269fa6098cc",
         "startDate":"2025-05-30",
      }
   }
}
'''

def json = new JsonSlurper().parseText(data)

json.value.scheduleElements = [json.value.scheduleElements]

def result = JsonOutput.prettyPrint(JsonOutput.toJson(json))

println result

when executed, prints:

─➤ groovy solution.groovy
{
    "value": {
        "ID": "1c1238c8-3517-47c7-83de-6269fa6098cc",
        "scheduleElements": [
            {
                "ID": "92a1352d-8319-4e1a-b921-0d7d0ee9f59e",
                "parentItem_ID": "1c1238c8-3517-47c7-83de-6269fa6098cc",
                "startDate": "2025-05-30"
            }
        ]
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.