Below is my scala array,
scala> val myarray = Array(("abc",25),("efd",20))
myarray: Array[(String, Int)] = Array((abc,25), (efd,20))
I would like to convert and write like below json format,
{"0":{"0":"abc","1":"efg"},
"1":{"0":25,"1":20}}
name/keys of json are formed from index of array,
0 1
0 (abc,25)
1 (efd,20)
Scala array is output of spark code, I'm trying to save it and use in downstream and this is toy data.
There are a bunch of JSON readers/writers for Scala, but you don't want JSON that corresponds directly to your arrays (for that you'd just use JSON arrays!).
Escaping strings is kind of annoying to do by hand, so the easiest way is to stick your data into a JSON data structure in some JSON library.
Personally, I like Jawn, which you'd use like so. Add the following to your build.sbt file (assuming you're using SBT):
resolvers += Resolver.sonatypeRepo("releases")
libraryDependencies += "org.spire-math" %% "jawn-parser" % "0.8.3"
libraryDependencies += "org.spire-math" %% "jawn-ast" % "0.8.3"
Then assuming you have
val myarray = Array(("abc",25),("efd",20))
pull out the pieces you want by hand and pack them into the jawn ast
import jawn.ast._
def arrAsObj[A, B <: JValue](arr: Array[A])(f: A => B) = JObject(
collection.mutable.ListMap(
arr.zipWithIndex.map{ case (a, i) => i.toString -> f(a) }: _*
)
)
// It's hard to do this in a loop unless you use Shapeless!
val ma0 = arrAsObj(myarray.map(_._1))(x => JString(x))
val ma1 = arrAsObj(myarray.map(_._2))(x => LongNum(x))
val json = arrAsObj(Array(ma0, ma1))(identity)
Which already has its .toString doing pretty much what you want:
scala> val json = arrAsObj(Array(ma0, ma1))(identity)
json: jawn.ast.JObject =
{"0":{"0":"abc","1":"efd"},"1":{"0":25,"1":20}}
You can find the case classes for the AST here to pick the right case class for a less-toy-like example.
This is far from the only way to do it, but unless you want to generate the JSON yourself (not terribly hard, but you probably at least want to use a JSON library to get the string escaping right), it will follow the same general form: put your data into a JSON tree structure, and let that produce the text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With