How to customize JSONBuilder results in Groovy? -



How to customize JSONBuilder results in Groovy? -

the below groovy code produces result shown under current result. how customize json output shown in expected output?

def resultset = sql.newinstance(...).rows('select * client') println new jsonbuilder(clients:resultset).toprettystring()

current result

{ "clients": [ { "id": 1, "name": "abc", "addr1": "123 main st", "addr2": "new york", "zip": "12345" }, { "id": 2, "name": "xyz", "addr1": "321 main st", "addr2": "new jersey", "zip": "98761" } ] }

do need loop through each row , customize?

expected

{ "clients": [ { "id": 1, "name": "abc", "contact": { "addr1": "123 main st", "addr2": "new york", "zip": "12345" } }, { "id": 2, "name": "xyz", "contact": { "addr1": "321 main st", "addr2": "new jersey", "zip": "98761" } } ] }

the code below should work (but i'm not sure cause there's no total illustration provided):

println new jsonbuilder(clients:resultset.collect {[ id: it.id, name: it.name, contact: [ addr1: it.addr1, addr2: it.addr2, zip: it.zip, ], ]}).toprettystring()

try , allow me know in case of problems. understand how works?

groovy

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -