3

I'm using scala and Json4s to add a JValue to a JArray in a loop, this way:

    var recordsJArray = JArray
    for (record <- recordsList) {
       val mrecord= new com.google.gson.Gson().toJson(record.asMap())
       val jsonRecord = parse(mrecord)
       recordsJArray = recordsJArray.++jsonRecord
    }

I've searched de api: https://static.javadoc.io/org.json4s/json4s-core_2.9.1/3.0.0/org/json4s/JsonAST$$JArray.html

and I've tried to use the method:

 def ++ (other: JValue): JValue

But it's not working.

 error: value ++ is not a member of object org.json4s.JsonAST.JArray
[ERROR]         recordsJArray = recordsJArray++jsonRecord

Can someone please help me? Is there any way of adding the JValue to the JArray? Thank you

jsonH
  • 61
  • 6
  • Looks like you are not using same version than the doc you've linked – OlivierBlanvillain May 04 '17 at 11:17
  • Hello, I'm using scala-2.10 and the version for json4s is 3.5.1 from maven repository. I've just realized the link is for 2.9.1 https://www.javadoc.io/doc/org.json4s/json4s-core_2.10/3.5.1 – jsonH May 04 '17 at 11:35
  • Great, you can now answer your own question by looking at the correct documentation! – OlivierBlanvillain May 04 '17 at 15:37
  • I can't find any function like ++ in documentation, that's why I've kept the question. I guess the function doesn't exist in my version, so I still need help to solve it – jsonH May 04 '17 at 19:15
  • BEWARE: Json4s is [vulnerable under DoS/DoW attacks](https://github.com/json4s/json4s/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+denial)! – Andriy Plokhotnyuk Dec 05 '19 at 07:43

1 Answers1

1

++ returns JValue, which is a supertype of JArray.

Since recordsJArray must be a JArray, the Scala compiler is looking for an implementation of ++ that returns a JArray. Since there is none, the compiler reports that your desired function is not a member of the object on which you're trying to invoke it.

Instead of relying on JsonAST's function, you can steal their implementation of ++ for your input types (JArray plus JValue):

recordsJArray = JArray(recordsJArray ::: List(jsonRecord))
crenshaw-dev
  • 7,504
  • 3
  • 45
  • 81