chris
chris

Reputation: 43

Convert a (String, List[(String, String)]) to JSON object

I have the data as:

(ID001,List((BookType,[text]),(author,xyz abc),(time,01/12/2019[22:00] CST/PM))),(ID002,List((BookType,[text]),(author,klj fgh),(time,19/02/2019[12:00] CST/AM)))

I need to convert this to a JSON object:

{"ID001":{
    "BookType":"[text]",
    "author":"xyz abc",
    "time":"01/12/2019[22:00] CST/PM" 
    },
{"ID002": {
    "BookType":"[text]" , 
    "author":"klj fgh", 
    "time":"19/02/2019[12:00] CST/AM"
    } 
}

I am very new to Scala Spark. Any idea how to convert this.

Upvotes: 1

Views: 312

Answers (1)

Guda uma shanker
Guda uma shanker

Reputation: 182

For creating the json use the jackson which has got good utilities.In your case the following done can be using the below code

      val mapper = new ObjectMapper`
      val data = ("ID01", List(("BookType", "[text]"),("author", "xyz abc"),"time","01/12/2019[22:00] CST/PM")))
      val id = mapper.createObjectNode()
      val bookDetails = mapper.createObjectNode()
      data._2.foreach(x => {bookDetails.put(x._1, x._2)})
      id.set(data._1, bookDetails)
      println(id.toString)

The output of the above will be as follows

{"ID01":{"BookType":"[text]","author":"xyz abc","time":"01/12/2019[22:00] CST/PM"}}

Upvotes: 0

Related Questions