My goal is to convert a DataFrame to a valid JSONArray of JSONObject.
I'm currently using:
val res = df.toJSON.collect()
But I'm getting an 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 for a way to convert those strings into actual JSONObjects, I found a few solution which suggested to find and replace characters, but I'm looking for something cleaner.
I tried to convert each string to a JSONObject using org.json library, but obviously it's not a Serializable Object.
Any suggestion? any fast Scala JSON library that can work?
Or how in general is it suggested to work with the toJSON method.
Update
This is a bit wasteful, but this option works for me:
val res = df.toJSON.map(new JSONObject(_).toString).collect()
Since JSONObject is not serializable - I can use its toString to get a valid JSON format.
If you still have any suggestion on how I can improve it - please let me know.