Read json key value but ignore object - scala

I have the below json
"atrr": {
"data": {
"id": "asfasfsaf",
"name": "cal",
"type": "state"
"ref": [
"xyz",
"uhz",
"arz"
]
}
}
I am reading this as below but not getting value k,v
def getData: Map[String, String] = (atrr \ "data").asOpt[Map[String, String]].getOrElse(Map[String, String]())
without ref it works fine.how do I ignore ref[] from json in code that is an object

You can use a custom Reads[Map[String, String]], passed to the .as or .asOpt method. My approach is to use Reads.optionNoError[String] to handle the values inside the main atrr object, so that any non-string field which would have caused an error will instead be treated as a None.
// explicitly construct something that'd normally be constructed implicitly
val customReads: Reads[Map[String, Option[String]] =
Reads.map(Reads.optionNoError[String])
// pass the custom reads explicitly with parens,
// instead of passing the expect type in square brackets
(atrr \ "data").asOpt(customReads)
This results in
Some(
Map(
id -> Some(asfasfsaf),
name -> Some(cal),
type -> Some(state),
ref -> None
)
)
which you can transform how you see fit, for example by doing
.map(_.collect { case (k, Some(v)) => k -> v })

Related

How to update JSON with arrays of objects?

I'm trying to make a transformation to every object in an array in my JSON.
The JSON looks something like this:
{
"foos": [
{
"urls": ["www.google.com", "www.google.com", "www.stackoverflow.com"]
},
{
"urls": ["www.facebook.com"]
},
...
]
}
I'm trying to remove duplicates from the urls array.
I've tried using something like this:
(__ \\ 'urls).json.update(Reads.of[JsArray].map(scopes => JsArray(scopes.value.distinct)))
But keep getting an error about error.path.result.multiple.
I can't seem to find a way to make the change to everything inside of a JSON array.
Also, I can't used case classes here because there are unknown fields on some of the data that I don't want to lose when converting to a case class.
In order to resolve that, we need to define 2 transformers. One acting on the internal object, to remove duplicates, and the other, to aggregate all objects.
The first one is:
val distinctTransformer = (__ \ "urls").json
.update(__.read[JsArray].map{o => {
JsArray(o.value.distinct)
}})
The other one is:
val jsonTransformer = (__ \ "foos").json
.update(__.read[JsArray].map(arr => {
JsArray(arr.value.map(_.transform(distinctTransformer))
.filter(_.isSuccess) // Error handling should be added here
.map(_.get)
)
}))
Then the usage is:
val json = Json.parse(jsonString)
json.transform(jsonTransformer) match {
case JsSuccess(value, _) =>
println(value)
case JsError(errors) =>
println(errors)
}
Code run at Scastie.

Scala foreach member variable

Is there a way to loop for each member variable of a class? It's trivial with Lists and arrays but I have to construct a case class with each json field mapping to a member variable to use the Play framework's Reads/validator API
Did you mean something like this:
case class SomeClass(a: Int, b: Int)
SomeClass(1,2).productIterator.foreach{ a => println(a)}
this will give you an output of: 1 2
Or if you are trying to construct an object from json. You can define reads in your case class which deserialises json to your object :
override def reads(json: JsValue): JsResult[SomeClass] = JsSuccess(SomeClass(
(json \ "a").as[Int],
(json \ "b").as[Int]
)
)
then to use the deserialisation:
val json = Json.obj() //Your json
json.validate[SomeClass]
json.fold(
errors => {
Json.obj("status" -> "Not OK", "message" -> JsError.toJson(errors))
},
preset => {
Json.obj("status" -> "OK")
}
)
If you want compare Json and get difference, may be better use JsObject methods?
For example fieldSet return all fields as a set. You can use diff on previous and current field set to get changed fields. This is fast solution and no any specific classes.

QueryString parsing in Play

I have nested url parameters being passed to an endpoint, and I need these represented in as a JsValue. My initial assumption was that Play would parse them in a way similar to Rails, however parameters seem to only be split by & and =. Example:
Query params: ?test[testkey]=testvalue&test[newkey]=newvalue
Actual:
Map(
"test[testkey]" -> "testvalue" ,
"test[newkey]" -> "newvalue
)
Expected:
Map(
"test" -> Map(
"testkey" -> "testvalue" ,
"newkey" -> "newvalue"
)
)
Note that the end goal here is to be able to convert this into a JsObject.
I've started writing this myself, however simply porting the function from Rack is very un-scala-y and I feel like there has to be a quick way to get this that I am simply missing.
UPDATE
I am trying to find a generic solution which mimics the parsing that Rails uses (ie, with nested objects, lists, etc), and not just one level deep objects.
Just for fun, one option is to do something like:
import scala.util.matching.Regex
val pattern = new Regex("""(\w+)\[(\w+)\]""")
val qs : Map[String, Map[String, List[Seq[String]]]] = request.queryString.toList.map {
case (k, v) =>
pattern findFirstIn k match {
case Some(pattern(key, value)) => (key, value, v)
}
}.groupBy(_._1).mapValues(value => value.groupBy(_._2).mapValues {
value => value.map(x => x._3)
})
To convert this is to a JsValue, we can simply invoke:
import play.api.libs.json.Json
Json.toJson(qs)
This assumes that all your url params look like map[key]=value. You would have to modify the code a little to accommodate the standard key=value pattern.

Converting JSON field names in argonaut

I'm writing a library to convert JSON responses from an API for backwards compatibility reasons. And what I need to do is take in arbitrary JSON, and change certain field names. I'm using scala and argonaut, but I don't see any way in the docs or examples of changing the FIELD names, only the values.
I don't know of a particularly nice way to do this, but it's not too awful to write a helper that will replace a field in an object and then use that in a cursor with withObject:
def renameField(before: JsonField, after: JsonField)(obj: JsonObject) =
obj(before).map(v => (obj - before) + (after, v)).getOrElse(obj)
Parse.parseOption("""{ "a": { "b": { "c": 1 } } }""").flatMap { json =>
(json.hcursor --\ "a").withFocus(_.withObject(renameField("b", "z"))).undo
}
This will return Some({"a":{"z":{"c":1}}}) as expected.
I ended up folding over the object I need to convert and adding to a map, and then creating a new json object.
val conversionMap = Map("a" -> "b")
Json(
j.objectOrEmpty.toMap.foldLeft(Map.empty[JsonField, Json]) {
case (acc, (key, value)) =>
acc.updated(conversionMap.getOrElse(key, key), j.fieldOrNull(key))
}.toSeq: _*
)

Specs2 JSONMatchers: mapping over Array elements?

I'm using the Specs2 JSONMatcher to validate that a GET request is being correctly converted from its internal representation (there are some manipulations we do before generating the JSON). What I need to do is, make sure that an element in the JSON array matches the corresponding object from our repository.
What I've tried:
val response = response.entity.asString // Spray's way of getting the JSON response
repository.all.map { obj =>
resp must */ ("id" -> obj.id)
resp must */ ("state" -> generateState(obj)
}
The problem is that the */ matcher just finds that "state": "whatever" (assuming generateState returns "whatever") exists somewhere in the JSON document, not necessarily in the same one matched by the ID
I tried using the indices but the repository.all method doesn't always return the elements in the same order, so there's no way of matching by index.
What I'd like to do is, iterate over the elements of the JSON array and match each one separately. Say an /## operator (or something) that takes matchers for each element:
resp /## { elem =>
val id = elem("id")
val obj = repository.lookup(id)
elem /("state" -> generateState(obj))
}
Does anyone have a way to do this or something similar?
Probably the easiest thing to do for now (until a serious refactoring of JsonMatchers) is to do some parsing and recursively use a JsonMatchers in a Matcher[String]:
"""{'db' : { 'id' : '1', 'state' : 'ok_1'} }""" must /("db" -> stateIsOk)
// a string matcher for the json string in 'db'
def stateIsOk: Matcher[String] = { json: String =>
// you need to provide a way to access the 'id' field
// then you can go on using a json matcher for the state
findId(json) must beSome { id: String =>
val obj = repository.lookup(id)
json must /("state" -> generate(obj))
}
}
// I am using my own parse function here
def findId(json: String): Option[String] =
parse(json).flatMap { a =>
findDeep("id", a).collect { case JSONArray(List(v)) => v.toString }
}
// dummy system
def generate(id: String) = "ok_"+id
case object repository {
def lookup(id: String) = id
}
What I did in the end is use responseAs[JArray], JArray#arr and JObject#values to convert the JSON structures into Lists and Maps, and then used the standard List and Map matchers. Much more flexible.