How to extract json missing fields? net.liftweb.json.MappingException: No usable value for ______

1,417 views
Skip to first unread message

45622576

unread,
Oct 9, 2013, 1:18:58 PM10/9/13
to lif...@googlegroups.com
I have many large JSON documents, and to save space, when a value does not exist, it's key is omitted.

Here's a scaled down example, where one of the addresses is missing a city:

  case class Address(city: String, state: String)
  case class Person(name: String, addresses: List[Address])
  implicit val formats = DefaultFormats

  val json = parse(""" { "name" : "Billy", "addresses" : [{"city": "New York", "state": "NY"}, {"state": "MA"}] } """)
  println(json.extract[Person])

But I keep getting errors like this:

                                                  //> net.liftweb.json.MappingException: No usable value for addresses
                                                  //| No usable value for city
                                                  //| Did not find value which can be converted into java.lang.String
                                                  //|     at net.liftweb.json.Meta$.fail(Meta.scala:191)
                                                  //|     at net.liftweb.json.Extraction$.mkValue$1(Extraction.scala:357)
                                                  //|     at net.liftweb.json.Extraction$.build$1(Extraction.scala:317)
                                                  //|     at net.liftweb.json.Extraction$$anonfun$12.apply(Extraction.scala:253)
                                                  //|     at net.liftweb.json.Extraction$$anonfun$12.apply(Extraction.scala:253)
                                                  //|     at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike
                                                  //| .scala:233)
                                                  //|     at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike
                                                  //| .scala:233)
                                                  //|     at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.
                                                  //| scala:59)
                                                  //|     at scala.collection.immutable.List.foreach(List.scala:76)
                                                  //|     at scala.collection.TraversableLike$class.map(TraversableLike.scala:233)

Is there a way to make lift-json pass in a null or empty string if a key doesn't exist? (rather than throwing an exception)

Or some other clean way to handle the many cases where a key is missing?

Thanks

Diego Medina

unread,
Oct 9, 2013, 1:31:17 PM10/9/13
to Lift
make the case class values Option instead of the Raw type, as in



  case class Address(city: Option[String], state: Option[String])


Thanks

  Diego



--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
 
---
You received this message because you are subscribed to the Google Groups "Lift" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liftweb+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Diego Medina
Lift/Scala consultant
di...@fmpwizard.com
http://fmpwizard.telegr.am
Reply all
Reply to author
Forward
0 new messages