amit
amit

Reputation: 356

Removing attributes from response of 'deep' JSON

In my grails controller i am returning my object like this :

JSON.use("deep") {
respond details
}

And the JSON I am getting is :

[
    {
        "class": "com.evolving.resource.tn.TNDetails",
        "id": null,
        "ageToDate": null,
        "dnpk": "1290",
        "iccid": [
            {
                "class": "com.evolving.resource.iccid.ICCID",
                "id": 4209,
                "imsi": [
                    {
                        "class": "com.evolving.resource.imsi.IMSI",
                        "id": 13336,
                        "iccid": {
                            "_ref": "../..",
                            "class": "com.evolving.resource.iccid.ICCID"
                        },
                        "imsi": "234207300009975"
                    }
                ],
                "sim": "8944200000060007084",
                "tn": {
                    "_ref": "../..",
                    "class": "com.evolving.resource.tn.TNDetails"
                }
            }
        ],
        "permanentReservedFlag": null,
        "portInOldSP": "XX",
        "portOutNewSP": null,
        "reserveToDate": null,
        "tn": "447400002035"
    }
]

How do i remove some unwanted tags like class , id , _ref from response JSON ?

I have used JsonRenderer in my resources.groovy file but it didn't worked.

Upvotes: 1

Views: 727

Answers (1)

amit
amit

Reputation: 356

Actually we just need to add a custom JSON marshler in bootstrap.groovy file like this : class BootStrap {

def init = { servletContext ->
    JSON.createNamedConfig("TNDetailsView", {
        JSON.registerObjectMarshaller(TNDetails) { TNDetails o ->
          return [
            tn : o.tn,
            sim : o.iccid.sim,
            imsi : o.iccid.imsi.imsi,
            ageToDate : o.ageToDate,
            permanentReservedFlag : o.permanentReservedFlag,
            portInOldSP : o.portInOldSP,
            portOutNewSP : o.portOutNewSP,
            reserveToDate : o.reserveToDate
          ]
        }
      })
}
def destroy = {
}
}

And in the controller just do :

respond details 

It will return only those attributes that we have mentioned in above in bootstrap file.

Upvotes: 1

Related Questions