I'm unable to parse the json I'm getting as a Map. Anyone have any ideas? Please do ask if you require any more information. Thanks :)
Trying to parse the following response using:
Json.parse(response.body).as[Map[String, Either[List[ErrorMsg], Seq[OepPoint]]]]
Response:
{
"Payout": {
"errors":[
{
"field": "Last point: OepPoint(0.033,72.14). Current: OepPoint(0.033,65.71)",
"message":"OEP must be unique"
}
],
"curve":[]
}
}
Error Message thrown is:
No Json deserializer found for type Map[String,Either[List[ErrorMsg],Seq[OepPoint]]]. Try to implement an implicit Reads or Format for this type.
[error] val errorExpected = Json.parse(response.body).as[Map[String, Either[List[ErrorMsg], Seq[OepPoint]]]]
[error] ^
[error] one error found
Structure of OepPoint:
case class OepPoint(oep: Double, loss: Double)
object OepPoint {
implicit val oepPointReads = Json.format[OepPoint]
}
Structure of ErrorMsg:
case class ErrorMsg(field: String, message: String)
object ErrorMsg {
implicit val errorMsgReads = Json.format[ErrorMsg]
}
Assuming you have proper Reads for OepPoint and ErrorMsg. You can do following.
case class ErrOrOep( errors: List[ ErrorMsg ], curve: List[ OepPoint ] )
implcit val errOrOepFormat = Json.format[ ErrOrOep ]
val jsonMap = Json.parse(response.body).as[ Map[String, ErrOrOep ] ]
val errOrOep = jsonMap( "Payout" )
val oepEither: Either[ List[ ErrorMsg ], List[ OepPoint ] ] =
( errOrOep.errors, errOrOep.curve ) match {
case ( _, h :: _ ) => Right( errOrOep.curve )
case ( h :: _, _ ) => Left( errOrOep.error )
case ( _, _ ) => Left( errOrOep.error )
}
Related
A circe noob here. I am trying to decode a JSON string to case class in Scala using circe. I want one of the nested fields in the input JSON to be decoded as a Map[String, String] instead of creating a separate case class for it.
Sample code:
import io.circe.parser
import io.circe.generic.semiauto.deriveDecoder
case class Event(
action: String,
key: String,
attributes: Map[String, String],
session: String,
ts: Long
)
case class Parsed(
events: Seq[Event]
)
Decoder[Map[String, String]]
val jsonStr = """{
"events": [{
"ts": 1593474773,
"key": "abc",
"action": "hello",
"session": "def",
"attributes": {
"north_lat": -32.34375,
"south_lat": -33.75,
"west_long": -73.125,
"east_long": -70.3125
}
}]
}""".stripMargin
implicit val eventDecoder = deriveDecoder[Event]
implicit val payloadDecoder = deriveDecoder[Parsed]
val decodeResult = parser.decode[Parsed](jsonStr)
val res = decodeResult match {
case Right(staff) => staff
case Left(error) => error
}
I am ending up with a decoding error on attributes field as follows:
DecodingFailure(String, List(DownField(north_lat), DownField(attributes), DownArray, DownField(events)))
I found an interesting link here on how to decode JSON string to a map here: Convert Json to a Map[String, String]
But I'm having little luck as to how to go about it.
If someone can point me in the right direction or help me out on this that will be awesome.
Let's parse the error :
DecodingFailure(String, List(DownField(geotile_north_lat), DownField(attributes), DownArray, DownField(events)))
It means we should look in "events" for an array named "attributes", and in this a field named "geotile_north_lat". This final error is that this field couldn't be read as a String. And indeed, in the payload you provide, this field is not a String, it's a Double.
So your problem has nothing to do with Map decoding. Just use a Map[String, Double] and it should work.
So you can do something like this:
final case class Attribute(
key: String,
value: String
)
object Attribute {
implicit val attributesDecoder: Decoder[List[Attribute]] =
Decoder.instance { cursor =>
cursor
.value
.asObject
.toRight(
left = DecodingFailure(
message = "The attributes field was not an object",
ops = cursor.history
)
).map { obj =>
obj.toList.map {
case (key, value) =>
Attribute(key, value.toString)
}
}
}
}
final case class Event(
action: String,
key: String,
attributes: List[Attribute],
session: String,
ts: Long
)
object Event {
implicit val eventDecoder: Decoder[Event] = deriveDecoder
}
Which you can use like this:
val result = for {
json <- parser.parse(jsonStr).left.map(_.toString)
obj <- json.asObject.toRight(left = "The input json was not an object")
eventsRaw <- obj("events").toRight(left = "The input json did not have the events field")
events <- eventsRaw.as[List[Event]].left.map(_.toString)
} yield events
// result: Either[String, List[Event]] = Right(
// List(Event("hello", "abc", List(Attribute("north_lat", "-32.34375"), Attribute("south_lat", "-33.75"), Attribute("west_long", "-73.125"), Attribute("east_long", "-70.3125")), "def", 1593474773L))
// )
You can customize the Attribute class and its Decoder, so their values are Doubles or Jsons.
I am using MongoDB scala driver. I have a problem with fetching record from MongoDB.
Following is my MongoDB initialization
private val client: MongoClient = MongoClient()
private val database: MongoDatabase = client.getDatabase(“rulemgntdb”)
val WorkOrdercollection: MongoCollection[Document] = database.getCollection("workOrder")
Find query :
MongoFactory.WorkOrdercollection.find().collect().subscribe(
(results: Seq[Document]) =>
println(s”Found: #${results}“)
)
Results printed like this :
Found: #List(Document((_id,BsonString{value=‘5af153f49547a205f9798129’}), (workOrderId,BsonString{value=‘9a9e1ce8-c576-4a15-a1ff-4af780b14b7f’}), (thingId,BsonString{value=‘Mumbai_Robot_3’}), (alertId,BsonString{value=‘Alert_1’}), (description,BsonString{value=‘Robot is not in good condition’}), (lastViewedDate,BsonDateTime{value=1525781377952}), (suggestedMaintenanceDate,BsonDateTime{value=1525781377952}), (startDate,BsonDateTime{value=1525781377952})))
I want to map this Document to my Case class.
Case class is like :
case class WorkOrder (
var id : String = (new ObjectId()).toString(),
var workOrderId: String,
var thingId : String,
var alertId : String,
var description : String,
val lastViewedDate : Date,
val suggestedMaintenanceDate : Date,
val startDate : Date
)
If I do following for getting JSON string from Document :
MongoFactory.WorkOrdercollection.find(query).subscribe(
(user: Document) => println(user.toJson()), // onNext
(error: Throwable) => println(s"Query failed: ${error.getMessage}"), // onError
() => println("Done") // onComplete
)
Then I will get Following JSON String:
{ “_id” : “5af153f49547a205f9798129", “workOrderId” : “9a9e1ce8-c576-4a15-a1ff-4af780b14b7f”, “thingId” : “Mumbai_Robot_3", “alertId” : “Alert_1", “description” : “Robot is not in good condition”, “lastViewedDate” : { “$date” : 1525781377952 }, “suggestedMaintenanceDate” : { “$date” : 1525781377952 }, “startDate” : { “$date” : 1525781377952 } }
I can Parse JSON string to case class but...Look at “startDate” : { “$date” : 1525781377952 } I am not able to Parse MongoDB Date to scala Date
How can I map Document to Case class?
You need to provide a custom codec for $date field. The following shows how it is done in play-json but the concept is similar in other JSON libraries:
object WorkOrder {
implicit val dateRead: Reads[Date] =
(__ \ "$date").read[Long].map(date => new Date(date))
implicit val dateWrite: Writes[Date] = new Writes[Date] {
def writes(date: Date): JsValue = Json.obj("$date" -> date.getTime)
}
implicit val codec = Json.format[WorkOrder]
}
You could use a JSON library.
In play-json
case class WorkOrder (
id: String,
workOrderId: String,
thingId: String,
alertId: String,
description: String,
lastViewedDate: Date,
suggestedMaintenanceDate: Date,
startDate: Date
)
object WorkOrder {
implicit lazy val fmt = Json.format[WorkOrder]
}
def documentToWorkOrder(doc: Document): WorkOrder = {
Json.parse(user.toJson().toString).validate[WorkOrder] match {
case JsSuccess(_, workOrderObj) => workOrderObj
case JsError(throwable) => throw throwable
}
}
//then in your code
MongoFactory.WorkOrdercollection.find(query).subscribe(
(user: Document) => documentToWorkOrder(user),
(error: Throwable) => println(s"Query failed: ${error.getMessage}"),
() => println("Done")
)
If I have a JSON object like:
{
"test": 3
}
Then I would expect that extracting the "test" field as a String would fail because the types don't line up:
import org.json4s._
import org.json4s.jackson.JsonMethods
import org.json4s.JsonAST.JValue
def getVal[T: Manifest](json: JValue, fieldName: String): Option[T] = {
val field = json findField {
case JField(name, _) if name == fieldName => true
case _ => false
}
field.map {
case (_, value) => value.extract[T]
}
}
val json = JsonMethods.parse("""{"test":3}""")
val value: Option[String] = getVal[String](json, "test") // Was Some(3) but expected None
Is this automatic conversion from a JSON numeric to a String expected in Json4s? If so, are there any workarounds for this where the extracted field has to be of the same type that is specified in the type parameter to the extract method?
This is the default nature of most if not all of the parsers. If you request a value of type T and if the value can be safely cast to that specific type then the library would cast it for you. for instance take a look at the typesafe config with the similar nature of casting Numeric field to String.
import com.typesafe.config._
val config = ConfigFactory parseString """{ test = 3 }"""
val res1 = config.getString("test")
res1: String = 3
if you wanted not to automatically cast Integer/Boolean to String you could do something like this manually checking for Int/Boolean types as shown below.
if(Try(value.extract[Int]).isFailure || Try(value.extract[Boolean]).isFailure) {
throw RuntimeException(s"not a String field. try Int or Boolean")
} else {
value.extract[T]
}
One simple workaround is to create a custom serializer for cases where you want "strict" behavior. For example:
import org.json4s._
val stringSerializer = new CustomSerializer[String](_ => (
{
case JString(s) => s
case JNull => null
case x => throw new MappingException("Can't convert %s to String." format x)
},
{
case s: String => JString(s)
}
))
Adding this serializer to your implicit formats ensures the strict behavior:
implicit val formats = DefaultFormats + stringSerializer
val js = JInt(123)
val str = js.extract[String] // throws MappingException
I have a JSON with the (simplified) format "Bar":[{"name":"Foo", "amount":"20.00"}, ...]
What do I need to do in order to find the element with the field amount equals to a number (e.g. 20.00) and return the name field?
I think the best solution is to use case class. I have written this small code, hope this helps.
// define below code in models package
case class Bar(name: String, amount: Double)
implicit val barReads: Reads[Bar] = (
(JsPath \ "name").read[String] and
(JsPath \ "amount").read[Double]
) (Bar.apply _)
// define below code in Application.scala
val jsonString =
"""
[{
"name": "MyName1",
"amount": 20.0
},
{
"name": "MyName2",
"amount": 30.0
}]
"""
val jValue = Json.parse(jsonString)
val Result = jValue.validate[Seq[Bar]].fold(errors => {
println(Json.obj("status" -> "Invalid Request!!", "message" -> JsError.toJson(errors)) + "\n")
},
input => { //input will return Seq[Bar]
for (i <- input) { // looping elements in Seq
if (i.amount == 20) {
println(i.name)
}
}
})
Reference: https://www.playframework.com/documentation/2.5.x/ScalaJson
Given the following JSON...
{ "id":"1234",
"name" -> "joe",
"tokens: [{
"id":"1234",
"id":"2345"
}]
}
... I need to replace the value of all the ids by xxxx like this:
{ "id":"xxxx",
"name" -> "joe",
"tokens: [{
"id":"xxxx",
"id":"xxxx"
}]
}
Let's start create the JSON tree:
val json = Json.obj(
"id" -> "1234",
"name" -> "joe",
"tokens" -> Json.arr(
Json.obj("id" -> "1234"),
Json.obj("id" -> "2345")
)
)
json: play.api.libs.json.JsObject = {"id":"1234","name":"joe","tokens":[{"id":"1234"},{"id":"2345"}]}
Then, getting all the ids is very simple:
json \\ "id"
res64: Seq[play.api.libs.json.JsValue] = List("1234", "1234", "2345")
Now, how do I replace the value of all the ids by xxxx?
There doesn't appear to be a nice way to do this with the standard Play JSON library, although I'd be happy to be proved wrong in that regard. You can however do it easily using the play-json-zipper extensions:
import play.api.libs.json._
import play.api.libs.json.extensions._
val json = Json.obj(
"id" -> "1234",
"name" -> "joe",
"tokens" -> Json.arr(
Json.obj("id" -> "1234"),
Json.obj("id" -> "2345")
)
)
// Using `updateAll` we pattern match on a path (ignoring
// the existing value, as long as it's a string) and replace it
val transformed = json.updateAll {
case (__ \ "id", JsString(_)) => JsString("xxxx")
}
// play.api.libs.json.JsValue = {"id":"xxxx","name":"joe","tokens":[{"id":"xxxx"},{"id":"xxxx"}]}
To make that a re-usable function:
def replaceValue(json: JsValue, key: String, replacement: String) = json.updateAll {
case (__ \ path, JsString(_)) if path == key => JsString(replacement)
}
The json-zipper extensions are still "experimental", but if you want to add them to your project add the following to your project/Build.scala appDependencies:
"play-json-zipper" %% "play-json-zipper" % "1.0"
and the following resolver:
"Mandubian repository releases" at "https://github.com/mandubian/mandubian-mvn/raw/master/releases/"
Probably it isn't most efficient way to do it, but you can try to convert your JSON to an object copy it with new fields and then convert it back to json. Unfortunately currently I don't have environment to check the code, but it should be something like this:
case class MyId(id: String)
case class MyObject(id: String, name: String, tokens: List[MyId])
implicit val idFormat = Json.format[MyId]
implicit val objectFormat = Json.format[MyObject]
val json = Json.parse(jsonString)
val jsResult = Json.fromJson[MyObject](json)
val obj = jsResult match {
case JsSuccess(s, _) => s
case _ => throw new IllegalStateException("Unexpected")
}
val newObj = obj.copy(id = "xxxx")
val result = Json.toJson(newObj)