Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Modifying JSON



We can modify JSON very easily in Groovy. Groovy provides JsonSlurper class as a primary object to parse a JSON string and stream it into Groovy objects. We can then modify JSON structure easily using Groovy Syntax. Another way is to parse existing JSON using JsonSlurper and then create another json with required modification using JsonBuilder. We're exploring both ways in this chapter with examples.

Modifying JSON using JsonSlurper

This is most easy and popular option to modify the JSON in groovy. We are parsing a JSON string using JsonSlurper class into a groovy object, employee and then operations are performed to update the employee object. Once changes are finalized, we're converting the employee object back to the json using JsonOutput class.

Example.groovy

import groovy.json.JsonOutput
import groovy.json.JsonSlurper

def jsonString = '''
{
   "name": "John",
   "age": 35,
   "city": "Dalas",
   "skills": ["Java", "Groovy", "Python"],
   "address": {
      "street": "Avenue 2",
      "zip": "60301"
   }
}
'''

def jsonSlurper = new JsonSlurper()
def employee = jsonSlurper.parseText(jsonString)

// update a simple value
employee.age = 31

// Add a new entry
employee.occupation = "Engineer"

// Update an element in a list
employee.skills[0] = "J2EE"

// Add an element to a list using left shift operator
employee.skills << "Angular"

// Update a nested object
employee.address.zip = "10002"

// Add a new field to a nested object
employee.address.country = "USA"

// Print the updated json after conversion
def updatedEmployee = JsonOutput.prettyPrint(JsonOutput.toJson(employee))
println updatedEmployee

Output

When we run the above program, we will get the following result.

{
    "address": {
        "country": "USA",
        "street": "Avenue 2",
        "zip": "10002"
    },
    "age": 31,
    "city": "Dalas",
    "name": "John",
    "occupation": "Engineer",
    "skills": [
        "J2EE",
        "Groovy",
        "Python",
        "Angular"
    ]
}

Modifying JSON using JsonBuilder

JsonBuilder provides more progammatic control over creation of JSON. We are first parsing the existing JSON string using JsonSlurper into a groovy object employee. Then using JsonBuilder, we are creating a new JSON structure while using values of employee object as shown below −

Example.groovy

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

def jsonString = '''
{
   "name": "John",
   "age": 35,
   "city": "Dalas",
   "address": {
      "street": "Avenue 2",
      "zip": "60301"
   }
}
'''

def jsonSlurper = new JsonSlurper()
def employee = jsonSlurper.parseText(jsonString)

def builder = new JsonBuilder()

builder {
   name employee.name
   age 31
   city employee.city
   occupation "Engineer"
   address {
      street "Avenue 2"
      zip "10002"
      country "USA"
   }
}

def modifiedJson = builder.toPrettyString()
println modifiedJson

Output

When we run the above program, we will get the following result.

{
    "name": "John",
    "age": 31,
    "city": "Dalas",
    "occupation": "Engineer",
    "address": {
        "street": "Avenue 2",
        "zip": "10002",
        "country": "USA"
    }
}
Advertisements