Reputation: 19
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",
}**]**
}
}
Upvotes: 0
Views: 549
Reputation: 4482
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"
}
]
}
}
Upvotes: 1