I need to retrieve information from a JsValue that may be structured in a few different ways buy the specific values I'm looking for will always be under similar names.

So for example I could have something like:

{
  "name" : "Watership Down",
  "location" : {
    "lat" : 51.235685,
    "long" : -1.309197
  }
}

OR

{
  "title" : "Watership Down",
  "size" : "M",
  "location" : {
    "latitude" : 51.235685,
    "longitude" : -1.309197
  }
}

and I'd want to be able to do: val text = json \\ "name"|"title"

I know what I want is either under name or title but not sure which in a given scenario. Is there any way to do something similar to what I did above with an "or" similar to Scala's .getOrElse() method?

1

There are 1 answers

0
Jean Logeart On BEST ANSWER

You can do:

val text = (json \\ "name").asOpt[String]
             .getOrElse((json \\ "title").as[String])