An SO User
An SO User

Reputation: 24998

Modify keys in Spray JSON

Suppose I have a JSON object as follows:

{ 
  "CATEGORY": "reference",
  "AUTHOR": "Nigel Rees",
  "TITLE": "Sayings of the Century",
  "PRICE": 8.95
}

This flows in the system as a JsObject. Using Spray, how would I lowercase these fields?

Upvotes: 1

Views: 455

Answers (1)

Emiliano Martinez
Emiliano Martinez

Reputation: 4133

This code:

import spray.json._

val str =
    """{"CATEGORY": "reference",
        "AUTHOR":  "Nigel Rees",
        "TITLE": "Sayings of the Century",
        "PRICE": 8.95}"""

  def main(args: Array[String]) : Unit =
    println(JsObject(str.parseJson.asJsObject.fields.map(el => el._1.asInstanceOf[String].toLowerCase -> el._2 )))

gives:

{"author":"Nigel Rees","category":"reference","price":8.95,"title":"Sayings of the Century"}

With the following dependence in sbt:

"com.typesafe.akka" %% "akka-http-spray-json" % "10.1.8"

Upvotes: 3

Related Questions