Groovy web console

subscribe to the feed Subscribe
to this
site
Json2Json Transformation (via #groovywebconsole)
tweet this snippet Tweet
this
script

Json2Json Transformation

Published 4 months ago by Kiran Patel with tags JsonBuilder Json2json
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.json.JsonBuilder

def response = """[   {
      "name": "John",
      "online_status": "online"
   },
   {
      "name": "Marry",
      "online_status": "online"
   }
]"""

def json = new groovy.json.JsonSlurper().parseText(response)

def json1 = new JsonBuilder()

json1 {   
    def array = []
    json.each{ item -> 
        array.add({  n item.name
            s item.online_status
        })
    }
    k array
}

println json1.toString()