Parsing and manipulating json in Scala Parsing and manipulating json in Scala json json

Parsing and manipulating json in Scala


You can convert the json into an array of case class which is the easiest thing to do. For example: you can have case class for Fields like

case class Field(`type`: String, name: String, value: String)

and you can convert your json into array of fields like read[Array[Field]](json) where json is

 [            {                "type": "standard",                "name": "fstname",                "value": "John"            },            ...        ]

which will give you an array of fields. Similarly, you can model for your entire Json.

As now you have an array of case classes, its pretty simple to iterate the objects and change the value using case classes copy method.

After that, to convert the array of objects into Json, you can simply use write(objects) (read and write functions of Json4s are available in org.json4s.native.Serialization package.

Update 

To do it without converting it into case class, you can use transformField function

parse(json).transformField{case JField(x, v) if x == "value" && v == JString("Company")=> JField("value1",JString("Company1"))}