scala - How to convert a Spark Dataframe to JSONObject -


my goal convert dataframe valid jsonarray of jsonobject.

i'm using:

val res = df.tojson.collect() 

but i'm getting array[string] - array of json-escaped strings, i.e:

["{\"url\":\"http://www.w3schools.com/html/html_form_action.asp?user=123\",\"subnet\":\"32.2.208.1\",\"country\":\"\",\"status_code\":\"200\"}"] 

i'm looking way convert strings actual jsonobjects, found few solution suggested find , replace characters, i'm looking cleaner.

i tried convert each string jsonobject using org.json library, it's not serializable object.

any suggestion? fast scala json library can work?

or how in general suggested work tojson method.

update

this bit wasteful, option works me:

 val res = df.tojson.map(new jsonobject(_).tostring).collect() 

since jsonobject not serializable - can use tostring valid json format.

if still have suggestion on how can improve - please let me know.

you use spray-json parse string case class:

import spray.json._ import defaultjsonprotocol._ case class data(url: string, subnet: string, country: string, status_code: string) implicit val dataformat = jsonformat4(data) val source = array("{\"url\":\"http://www.w3schools.com/html/html_form_action.asp?user=123\",\"subnet\":\"32.2.208.1\",\"country\":\"\",\"status_code\":\"200\"}") val data = source(0).parsejson.convertto[data] 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -