1

how easily i can transform data ? I can searching data which interesting me, f. ex:

json \\ fieldName1 \\ fieldName2 \\ fieldName3

But how i can of this search modify value? f. ex

json transform{
  case JField(x,y) => JField(x, z)
}
  • edit: I try: getting fields from query and then for each Jfield call method transform where i checked if current field is field from query. But this is very ugly – user1511848 Jul 09 '12 at 12:41
  • 1
    Could you use ``extract`` (cf. the lift-json docs https://github.com/lift/lift/tree/master/framework/lift-base/lift-json/, section "Extracting values") in order to transform the json AST into a list of case classes, and then use the usual collection methods such as ``filter`` and ``map`` in order to do the transformation? – Malte Schwerhoff Jul 09 '12 at 13:54

1 Answers1

1

If you use lift-json, you get exactly what you want :

scala> import net.liftweb.json._
scala> import net.liftweb.json.JsonDSL._

scala> val json = 
  ("person" ->
    ("name" -> "Joe") ~
    ("age" -> 35) ~
    ("spouse" -> 
      ("person" -> 
        ("name" -> "Marilyn") ~
        ("age" -> 33)
      )
    )
  )
scala> json transform {
         case JField("name", JString(s)) => JField("NAME", JString(s.toUpperCase))
       }
res8: net.liftweb.json.JsonAST.JValue = JObject(List(JField(person,JObject(List(
JField(NAME,JString(JOE)), JField(age,JInt(35)), JField(spouse,JObject(List(
JField(person,JObject(List(JField(NAME,JString(MARILYN)), JField(age,JInt(33)))))))))))))

The above codes are copied from the linked page.

If you don't use lift-json, you may take a look at kiama, as demonstrated in this answer.

Community
  • 1
  • 1
xiefei
  • 6,563
  • 2
  • 26
  • 44