0

I have a list of documents, with data in the form of Map[String, Any] so that it can be added to a json object with fields and values respectively. So my code looks something like this when parsing:

val JsonDoc = Json.obj(
      "ORFID" -> doc("ORFID").toString,
      "ORF_len" -> doc("ORF_len").toString,
      "start" -> doc("start").toString,
      "end" -> doc("end").toString
...// more fields
)

However some documents may be missing one of the fields, so I need a more dynamic approach depending on each documents fields, or else an exception is thrown. What would be the best approach for this?

1
  • Having Any as value type of the Map is not a very good idea Commented Jun 6, 2016 at 20:13

1 Answer 1

2

You could try JsObject instead. Its constructor needs a Seq of tuples which you can get from your map with the method toSeq, this assuming, you want to use the same keys in your json as in the Map.

val jsonDoc = new JsonObj(doc.toSeq)
Sign up to request clarification or add additional context in comments.

1 Comment

unfortunately, its a map of (String, Any) and the required typing is (String, JsValue), I resorted to use getOrElse on the fields that could potentially by empty to avoid the error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.