yonsei
yonsei

Reputation: 13

Remove JSON array element in scala

I have a JSON result like this:

[
    {
        "id": 2202,
        "name": "name one",
        "phone": "+62888888",
        "email": "[email protected]",
        "corporateId": null,
        "corporateName": null,
        "version": 119,
        "signupDate": "2016-01-28T00:00:00+07:00"
    },
    {
        "id": 2580,
        "name": "name two",
        "phone": "+628777777",
        "email": "[email protected]",
        "corporateId": null,
        "corporateName": null,
        "version": 119,
        "signupDate": "2016-01-28T00:00:00+07:00"
    }
]

How to remove some element so I have new JSON result like this (remove corporateId and corporateName):

[
    {
        "id": 2202,
        "name": "name one",
        "phone": "+62888888",
        "email": "[email protected]",
        "version": 119,
        "signupDate": "2016-01-28T00:00:00+07:00"
    },
    {
        "id": 2580,
        "name": "name two",
        "phone": "+628777777",
        "email": "[email protected]",
        "version": 119,
        "signupDate": "2016-01-28T00:00:00+07:00"
    }
]

Upvotes: 1

Views: 1084

Answers (1)

andrey.ladniy
andrey.ladniy

Reputation: 1674

There is some ways. One of them JSON transformers.

For removing corporateId and corporateName use Case 6: Prune a branch from input JSON:

(__ \ "corporateId").json.prune andThen (__ \ "corporateName").json.prune

For removing in every JsArray element, use Reads.list:

Reads.list(
  (__ \ "corporateId").json.prune andThen (__ \ "corporateName").json.prune
)

Complex transformer for you will be:

json.transform(
  Reads.list(
    (__ \ "corporateId").json.prune andThen (__ \ "corporateName").json.prune
  ).map(JsArray)
)

res3: play.api.libs.json.JsResult[play.api.libs.json.JsArray] = JsSuccess([{"id":2202,"name":"name one","phone":"+62888888","email":"[email protected]","version":119,"signupDate":"2016-01-28T00:00:00+07:00"},{"id":2580,"name":"name two","phone":"+628777777","email":"[email protected]","version":119,"signupDate":"2016-01-28T00:00:00+07:00"}],)

Upvotes: 3

Related Questions