Groovy web console

subscribe to the feed Subscribe
to this
site

Add Empty Array to JSON object

Published 4 weeks ago by Anonymous
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.json.*    
def json_req = '''[ { "clientId": "00001", "name": "Client 1" } ]'''
    
def json = new JsonSlurper().parseText(json_req)
println JsonOutput.toJson(json)
if (json[0].get("offices") == null)
    json[0] << [offices: []]
println JsonOutput.toJson(json)