Reputation: 141
I am writing a code to get JSON data from a web API and ultimately want to put it into HBASE
table using Phoenix
.
Initial piece of code that I am using to get data from web API is returning me data in form of a string:
class Health() {
def getHealthData() {
val userRDD = df.select("ID", "PERMISSION_T", "UPDATE_T", "HEALTHTID").rdd;
val newData = userRDD.map(
(
row => {
{
var ID = row(0).toString // User ID
var permT = row(1).toString // access token
var rT = row(2).toString // refresh token
var healthID = row(3).toString // fitbit user id
var forDate = "2018-12-04"
val hUrl = s "web api url"
try {
--Piece of code to connect to web api-- --
hJsonStr = heartHandler.handleResponse(heartResponse).trim
println(s "=============In TRY(Print hJsonStr) ============$hJsonStr")
}
heartClient.getConnectionManager().shutdown();
} catch {
case unauthorized:
Exception => {
println(s "Unknown exception: $hUrl")
RecoverToken()
}
}
def RecoverToken(): Unit = {
println("<<<<<<<<<< Recover Token >>>>>>>>>")
val recoverUrl = "recover url"
}
//heartJson
//heartJsonStr
"ID:" + ID + s "--$hUrl -- $hJsonStr -- " //+ accessToken //+ "--" + activitiesJsonStr
};
}
)
);
newData.collect.foreach {
println
}
val fb = new Fitbit(); // initialize new fitbit object
fb.getFitBitData(); // call function to fetch data
Now I want to convert this string having inbuilt JSON values to a proper JSON object so that I can put it into HBASE
. I need to do this using Spark, Scala on Zappelin.
Below is sample data I am getting from web API using above code:
ID:XYZ1--web url -- {"activities-heart":[{"dateTime":"2018-12-18","value":{"customHeartRateZones":[],"heartRateZones":[{"caloriesOut":714.31496,"max":88,"min":30,"minutes":667,"name":"Out of Range"},{"caloriesOut":240.01076,"max":123,"min":88,"minutes":66,"name":"Fat Burn"},{"caloriesOut":0,"max":150,"min":123,"minutes":4,"name":"Cardio"},{"caloriesOut":0,"max":220,"min":150,"minutes":0,"name":"Peak"}],"restingHeartRate":62}}]} -- ID:XYZ2--web url -- --
I have tried importing few JSON
libraries and used below code but it failed due to syntax errors:
import org.json4s.jackson.JsonMethods._
val parsed = parse(newData).asInstanceOf[JObject]
parse(newData).asInstanceOf[JObject]
Upvotes: 0
Views: 521
Reputation: 141
After suggestion from @vindev, I apparently realized I needed to remove additional string before trying to convert JSON string to JSON object. Once I did that I was able to convert it inside the loop. I used below piece of code after getting the value of hJsonStr
in the code:
hJsonStr = heartHandler.handleResponse(heartResponse).trim
import org.json4s.jackson.JsonMethods._
val parsedHJsonStr = parse(hJsonStr)
Upvotes: 1