Laurence Geng
Laurence Geng

Reputation: 434

How to modify value type of a json via Circe

I need capture int values of a json and change it to a string value by a certain mapping table. I use circe, i had known how to modify a value without changing value type, for example:

package com.accenture.soh.driver

import io.circe._, io.circe.parser._
import cats.syntax.either._
import io.circe._, io.circe.parser._
import io.circe.optics.JsonPath._

/**
  * Created by laurence.geng on 2017/2/2.
  */
object CirceTest extends App {

  val json: Json = parse(
    """
  [
  {
    "metric":"my-metric",
    "dps":{"1484214238":5,"1484214239":1,"1484214240":4,"1484214241":11}
  }
  ]
  """).getOrElse(Json.Null)

  val doubleValue: Json => Json = root.each.dps.each.int.modify(_ * 2)

  println(doubleValue(json).toString())

}

The output is:

[
  {
    "metric" : "my-metric",
    "dps" : {
      "1484214238" : 10,
      "1484214239" : 2,
      "1484214240" : 8,
      "1484214241" : 22
    }
  }
]

But, actully, what i need to do is change the int value a certain string value, i.e. 1 -> "A", 2 -> "B", and so on, the problem is: the "modify" method only accept a function which return the same type to input value, so, i can NOT code as following:

val intToString: Json => Json = root.each.dps.each.int.modify(_.toString)

and my expected output may looks as following:

[
  {
    "metric" : "my-metric",
    "dps" : {
      "1484214238" : "10",
      "1484214239" : "2",
      "1484214240" : "8",
      "1484214241" : "22"
    }
  }
]

Can anybody give me a workround (based on Circe)?

Upvotes: 3

Views: 3203

Answers (2)

tonykoval
tonykoval

Reputation: 1230

Naive solution:

val doubleValue: Json => Json = root.each.dps.each.json.modify(x =>
  Json.fromString(
    (x.asNumber.getOrElse(throw new Exception("ups")).truncateToInt * 2).toString
  )
)

Upvotes: 1

Bradley Kaiser
Bradley Kaiser

Reputation: 774

You can do it with circe cursors, but i think its easier to just convert to a case class and operate on that.

 val json =
  """
    |{
    |    "metric":"my-metric",
    |    "dps":{"1484214238":5,"1484214239":1,"1484214240":4,"1484214241":11}
    |  }
  """.stripMargin

val doc = parse(json).getOrElse(Json.Null)
val cursor = doc.hcursor
val dps = cursor.downField("dps")
val result = dps.withFocus { json: Json =>
  json.mapObject { jsonObject =>
    JsonObject.fromMap(jsonObject.toMap.mapValues { x =>
      Json.fromString(x.asNumber.get.toString)
    })
  }
}

Upvotes: 5

Related Questions