0

I know this kind of question has been answered so many times, but i could not find the answer to the error I am getting ::

I am trying to convert a string JSON into a case class in IntelliJ IDEA CE.

The code goes like this ::

package com.netflix.ist.gbi.application

import scala.io.Source
import scala.collection.mutable._
import org.json4s._
//import org.json4s.DefaultFormats
import com.netflix.ist.gbi.model.EventPayloadIn
import org.json4s.jackson.JsonMethods._

object ObjDataArchival extends App{
    val a : ListBuffer[String] = ListBuffer()
    for (line Source.fromFile("/Users/sankar.biswas/Desktop/jsonFile.json").getLines) {
       a.append(line)
       jsonStrToMap(line)
    }


    def jsonStrToMap(jsonStr: String) : EventPayloadIn = {
       implicit val formats : DefaultFormats.type = org.json4s.DefaultFormats
       parse(jsonStr).extract[EventPayloadIn]
    }


}

The case classes are defined in a scala file which has been imported. but when I am running it, I am getting the below error ::

Exception in thread "main" java.lang.NoSuchMethodError: scala.Function0.$init$(Lscala/Function0;)V
at org.json4s.ThreadLocal.<init>(Formats.scala:348)
at org.json4s.DefaultFormats.$init$(Formats.scala:355)
at org.json4s.DefaultFormats$.<init>(Formats.scala:333)
at org.json4s.DefaultFormats$.<clinit>(Formats.scala)
at com.apple.ist.gbi.application.ObjDataArchival$.jsonStrToMap(ObjDataArchival.scala:21)
at com.apple.ist.gbi.application.ObjDataArchival$$anonfun$1.apply(ObjDataArchival.scala:16)
at com.apple.ist.gbi.application.ObjDataArchival$$anonfun$1.apply(ObjDataArchival.scala:13)
at scala.collection.Iterator$class.foreach(Iterator.scala:893)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
at com.apple.ist.gbi.application.ObjDataArchival$.delayedEndpoint$com$apple$ist$gbi$application$ObjDataArchival$1(ObjDataArchival.scala:13)
at com.apple.ist.gbi.application.ObjDataArchival$delayedInit$body.apply(ObjDataArchival.scala:10)
at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at com.apple.ist.gbi.application.ObjDataArchival$.main(ObjDataArchival.scala:10)
at com.apple.ist.gbi.application.ObjDataArchival.main(ObjDataArchival.scala)

The error seems to be in this line ::

implicit val formats : DefaultFormats.type = org.json4s.DefaultFormats

I tried with similar lines like ::

implicit val formats = DefaultFormats

But could not resolve the issue in any way.

Thanks in advance!

1 Answer 1

2

Something like below can work.

import org.json4s._
import org.json4s.jackson.JsonMethods._
import scala.io.Source

object JSONParsing extends App {


  implicit val formats = DefaultFormats // Brings in default date formats etc.

  case class BookDetails(bookId: String, bookName: String, authorName: String, authorCountry: String)

  for (line <- Source.fromFile("/Users/sankar.biswas/Desktop/jsonFile.json").getLines) {
    val bookDetails = jsonStrToMap(line)
    println(bookDetails)
  }

  def jsonStrToMap(jsonStr: String): BookDetails = {
    parse(jsonStr).camelizeKeys.extract[BookDetails]
  }
}

Content of jsonFile file :

{"book_id":"1","book_name":"Scala","author_name":"Edward","author_country":"Poland"}
{"book_id":"1","book_name":"Scala","author_name":"Edward","author_country":"Poland"}
{"book_id":"1","book_name":"Scala","author_name":"Edward","author_country":"Poland"}
{"book_id":"1","book_name":"Scala","author_name":"Edward","author_country":"Poland"}

Update :

build.sbt

  "org.json4s" %% "json4s-native" % "3.5.2",
  "org.json4s" %% "json4s-jackson" % "3.6.0"
Sign up to request clarification or add additional context in comments.

7 Comments

Hi @KZapagol, thank you so much for taking the time, tried your suggestion but getting the same error, but while I am trying to run the code in Scala REPL, it is working fine, it has something to do with IntelliJ i suppose! still unable to figure the error out.
Hi..I also used IntelliJ and same code is working fine. Can you please share your case class structure and json content.
ok before sending the case class and content, let me inform you this that i have tried running the object "JSONParsing" which you have created in my IntelliJ as well, and i am getting the same error.
It's strange..did you use JSONParsing code as it is or modified something?Are you using same json file content?
Yes I am using the code as it is. I think it has something to do with the IntelliJ, or may be the version that i am using for different components are not compatible with each other!
|

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.