Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Json values as Map with lift-json

The documentation for lift-json suggests that I should be able to call 'values' to get my current JObject structure as a vanilla Scala Map. This approach is not working for me, as the return type of 'values' is json.Values rather than a Map as the examples show. What am I doing wrong? Is there an implicit import necessary to accomplish this conversion?

scala> val json = parse("""{"k1":"v1","k2":"v2"}""")         
json: net.liftweb.json.package.JValue = JObject(List(JField(k1,JString(v1)), JField(k2,JString(v2))))

scala> json.values                                  
res4: json.Values = Map((k1,v1), (k2,v2))

scala> res4.get("k1")                                        
<console>:18: error: value get is not a member of json.Values
   res4.get("k1")
like image 952
Janx Avatar asked Apr 06 '11 18:04

Janx


1 Answers

Somehow I missed the duplicate of this in my search: Can I use the Scala lift-json library to parse a JSON into a Map?

Answer is to cast explicitly:

json.asInstanceOf[JObject].values
like image 129
Janx Avatar answered Nov 05 '22 18:11

Janx