How can i modify a json file in scala spark play

8 views
Skip to first unread message

Maurizio Diserio

unread,
Oct 19, 2016, 12:50:30 PM10/19/16
to Play Framework

I want to read a JSON file and create a class/object that saved all label and value from every JSON's vector/record. Then I want to modify  the JSON structure and get this modified JSON file to Http request with play/spark/scala.


For example, I have this JSON file


   [
  {
    "ser": 345,
    "City": "New York",
    "Gen": 1
  },
  {
    "ser": 55,
    "City": "New York",
    "Gen": 2
  },
  {
    "ser": 19,
    "City": "New York",
    "Gen": 3
  }
 ]



I create the class that take the json (with format method):


case class Book(ser: Int, city: String, gen: Int)


Format method: 


implicit val bookFormat: Format[Book] = {
  ((JsPath \ "ser").format[Int] and
    (JsPath \ "City").format[String] and
    (JsPath \ "Gen").format[Int]
    ) (Book.apply, unlift(Book.unapply))
}

// returns a sequence of Book objects
val books = Json.parse(bookJson).as[Seq[Book]]


Now I can:

books(9).city //return the city's value of the seq Book in 9 position of the seq


Ok so now i want to create a new Json with this structure that the values from the input Json: (I want output like this)


[ { "label_gen": [1,2,3] //that is the Gen value, 
     "values_gen": [ 
                 { "label":"New York" 
                   "values":["200","10","66"]  //is the ser about all Gen (for eexample 200 is ser is for label/gen 1 .. 10 is for label/gen 2 etc.
                 },
                { "label":"London" 
                  "values":["500","150","46"] 
                }, 
                { "label":"London" 
                  "values":["500","150","46"] 
                }, 
                .
                . 
                . 
                . 
                . 
               ] 
  } 
]

That is different from the structor of the JSON input. How can modify the Json in input?


After this, i will quering the new Json with spark, and answer with it to an Http request.

I hope I was understood now :D

Reply all
Reply to author
Forward
0 new messages