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>
```js
var getData = {
  "Status": "Fail",
  "Code": "500",
  "Rules": [{
      "Status": "Fail",
      "Message": "Code error",
      "id": "123456"
    },
    {
      "Status": "Fail",
      "Message": "Configuration error",
      "id": "12345"
    }
  ]
};
delete getData.Code;
for (var i = 0; i < getData.Rules.length; i++) {
  delete getData.Rules[i].id;
}
console.log(getData);
```

Note:- You can simply use `delete elementValue` to achieve that