Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert JValue to Map[String, Any] #124

Closed
lovesh opened this issue May 5, 2014 · 8 comments
Closed

Convert JValue to Map[String, Any] #124

lovesh opened this issue May 5, 2014 · 8 comments

Comments

@lovesh
Copy link

lovesh commented May 5, 2014

There should be method to convert the parsed json string to a Map, like most languages have. Right now i have to make a case class and then convert it to a map.

@casualjim
Copy link
Member

you can extract a Map[String, Any].

@lovesh
Copy link
Author

lovesh commented May 5, 2014

extracting with a Map[String, Any] gives an exception.
eg.
val json = parse(""" { "name": "joe", "children": [ { "name": "Mary", "age": 5 }, { "name": "Mazy", "age": 3 } ] } """)

Now
json.extract[Map[String, Any]] gives exception

org.json4s.package$MappingException: No information known about type at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$instantiate(Extraction.scala:465) at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$result$6.apply(Extraction.scala:491) at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$result$6.apply(Extraction.scala:488) at org.json4s.Extraction$.org$json4s$Extraction$$customOrElse(Extraction.scala:500) at org.json4s.Extraction$ClassInstanceBuilder.result(Extraction.scala:488) at org.json4s.Extraction$.extract(Extraction.scala:332) at org.json4s.Extraction$$anonfun$extract$5.apply(Extraction.scala:316) at org.json4s.Extraction$$anonfun$extract$5.apply(Extraction.scala:316) at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) at scala.collection.immutable.List.foreach(List.scala:318) at scala.collection.TraversableLike$class.map(TraversableLike.scala:244) at scala.collection.AbstractTraversable.map(Traversable.scala:105) at org.json4s.Extraction$.extract(Extraction.scala:316) at org.json4s.Extraction$.extract(Extraction.scala:42) at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21) at .<init>(<console>:18) at .<clinit>(<console>) at .<init>(<console>:7) at .<clinit>(<console>) at $print(<console>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734) at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983) at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573) at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604) at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568) at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:756) at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:801) at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:713) at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:577) at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:584) at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:587) at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:878) at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833) at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833) at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135) at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:833) at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:900) at xsbt.ConsoleInterface.run(ConsoleInterface.scala:69) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:102) at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:77) at sbt.Console.sbt$Console$$console0$1(Console.scala:23) at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24) at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply(Console.scala:24) at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply(Console.scala:24) at sbt.Logger$$anon$4.apply(Logger.scala:90) at sbt.TrapExit$App.run(TrapExit.scala:244) at java.lang.Thread.run(Thread.java:745)

@casualjim casualjim reopened this May 5, 2014
@lovesh
Copy link
Author

lovesh commented May 6, 2014

Is there some other way of converting JValue to Map[String, Any]?

@fedeoasi
Copy link
Contributor

I believe I fixed this in #126

@jarandaf
Copy link

jarandaf commented Jan 9, 2015

On 3.2.11 release seems to be working fine.

@ziziermao
Copy link

Hey, I still have this problem when trying to do parse(entry).extract[Map[String, Any]]. Any suggestions?

@ziziermao
Copy link

Hey guys,
I am trying having this problem when doing
parse(entry).extract[Map[String, Any]]
I got:
org.json4s.package$MappingException: No information known about type

@fedeoasi
Copy link
Contributor

@ziziermao: Extracting a Map[String, Any] works fine for me on the latest version (3.4.0). Could you share the json fragment that you're trying to extract and the version of json4s that you are using?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants