Json coast to coast with missing branches

168 Views Asked by At

EDIT: I made a Github repo to show the problem:

https://github.com/martinpallmann/json-coast-to-coast


I have the following Json-structure:

{ 
  "first_name" : "Martin",
  "address" : {
    "street" : "5th Ave",
    "town" : "Big Apple"
  }
}

I want to convert it to the following Json using Coast-to-coast-transformers (http://www.playframework.com/documentation/2.1.3/ScalaJsonTransformers):

{
  "first" : "Martin",
  "address_street" : "5th Ave",
  "address_town" : "Big Apple"
}

That's just an example. The real data structures are a bit different. But I think this example will suffice.

Here's what I did:

import play.api.libs.json._
import play.api.libs.functional.syntax._
import play.api.libs.json.Reads._


val t = ( __ \ 'first).json.copyFrom(( __ \ 'first_name).json.pick) and 
( __ \ 'address_street).json.copyFrom(( __ \ 'address \ 'street).json.pick) and
( __ \ 'address_town).json.copyFrom(( __ \ 'address \ 'town).json.pick) reduce

json.transform(t)

For the current example it works, but if I have the following source json:

{ "first_name" : "Nobody" }

Then I'd like to have the result:

{ "first" : "Nobody" }

instead of the JsError.

Could anybody tell me how to achieve this?

0

There are 0 best solutions below