scala - Convert RDD to JSON Object -


i have rdd of type rdd[(string, list[string])].

example:

(fruit, list(apple,banana,mango)) (vegetable, list(potato,tomato)) 

i want convert above output json object below.

{   "categories": [     {       "name": "fruit",       "nodes": [         {           "name": "apple",           "isintoplist": false         },         {           "name": "banana",           "isintoplist": false         },         {           "name": "mango",           "isintoplist": false         }       ]     },     {       "name": "vegetable",       "nodes": [         {           "name": "potato",           "isintoplist": false         },         {           "name": "tomato",           "isintoplist": false         },       ]     }   ] } 

please suggest best possible way it.

note: "isintoplist": false constant , has there every item in jsonobject.

first used following code reproduce scenario mentioned:

val samplearray = array( ("fruit", list("apple", "banana", "mango")), ("vegetable", list("potato", "tomato")))  val samplerdd = sc.parallelize(samplearray) samplerdd.foreach(println) // printing result 

now, using json4s scala library convert rdd json structure requested:

import org.json4s.native.jsonmethods._ import org.json4s.jsondsl.withdouble._  val json = "categories" -> samplerdd.collect().tolist.map{ case (name, nodes) =>   ("name", name) ~   ("nodes", nodes.map{     name => ("name", name)   }) }  println(compact(render(json))) // printing rendered json 

the result is:

{"categories":[{"name":"fruit","nodes":[{"name":"apple"},{"name":"banana"},{"name":"mango"}]},{"name":"vegetable","nodes":[{"name":"potato"},{"name":"tomato"}]}]} 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -