Question: I'm trying to filter out some fields in my JSON response.
Sample response below:

```
{
  "Status": "Fail",
  "Code": "500",
  "Rules": [{
      "Status": "Fail",
      "Message": "Code error",
      "id": "123456"
    },
    {
      "Status": "Fail",
      "Message": "Configuration error",
      "id": "12345"
    }
  ]
}

```

I want to omit code and id fields and print rest of the fields in JSON.
the final response should look like this:-

```
{
  "Status": "Fail",
  "Rules": [{
      "Status": "Fail",
      "Message": "Code error"
    },
    {
      "Status": "Fail",
      "Message": "Configuration error"
    }
  ]
}

```

Any idea on how we can achieve it?

Answer: 
Groovy already provide classes and mechanisms to do this, first you need to import class **groovy.json.JsonGenerator**

Then you can define the fields that you want to ignore from serilization:

```
def generator = new JsonGenerator.Options()
    .excludeFieldsByName('id', 'Code')
    .build()

```

And finally just need to parse the output:

```
String output = generator.toJson(input)

```

The output will look something like this:

```json
{
    "Status": "Fail",
    "Rules": [
        {
            "Status": "Fail",
            "Message": "Code error"
        },
        {
            "Status": "Fail",
            "Message": "Configuration error"
        }
    ]
}

```

Here is a full sample of how I did this:

```
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import groovy.json.JsonGenerator

String json = '''{
  "Status": "Fail",
  "Code": "500",
  "Rules": [{
      "Status": "Fail",
      "Message": "Code error",
      "id": "123456"
    },
    {
      "Status": "Fail",
      "Message": "Configuration error",
      "id": "12345"
    }
  ]
}'''

Map input = new JsonSlurper().parseText(json)

def generator = new JsonGenerator.Options()
    .excludeFieldsByName('id', 'Code')
    .build()

String output = generator.toJson(input)

println JsonOutput.prettyPrint(output)

```

To see more configurations for this **JsonGenerator** class you can go to official documentation here: <https://groovy-lang.org/json.html#_customizing_output>
A solution which recursively walks through your json and removes specific fields on any depth:

```
import groovy.json.*

def str = '''
{
  "Status": "Fail",
  "Code": "500",
  "Rules": [{
      "Status": "Fail",
      "Message": "Code error",
      "id": "123456"
    },
    {
      "Status": "Fail",
      "Message": "Configuration error",
      "id": "12345"
    }
  ]
}'''

def json  = new JsonSlurper().parseText(str)
def clean = recursivelyRemove(json, ['id', 'Code'])

println JsonOutput.prettyPrint(JsonOutput.toJson(clean))

def recursivelyRemove(obj, fieldNames) {
  switch(obj) {
    case Map:
      obj.findAll { k, v -> 
        !(k in fieldNames)
      }.collectEntries { k, v -> 
        [k, recursivelyRemove(v, fieldNames)]
      }
      break
    case List:
      obj.collect { recursivelyRemove(it, fieldNames) }
      break
    default: 
      obj
  }
}

```

which prints:

```

─➤ groovy solution.groovy                                                                                              1 ↵
{
    "Status": "Fail",
    "Rules": [
        {
            "Status": "Fail",
            "Message": "Code error"
        },
        {
            "Status": "Fail",
            "Message": "Configuration error"
        }
    ]
}

```

when run.

This has the upside that it is not hard-coded to your json structure, i.e. if the structure changes for some reason, this code might still work.

A potential downside is that if your json structure is very deep (say hundreds or thousands of levels of nesting), you might get a StackOverflowException as we are making recursive calls. This might or might not be likely depending on your scenario.