I have a flask application that returns the following if I do a cURL to it:
The response is in a JSON format as follows:
{"your_field": "hello there buddy"}
I want to get just the value "hello there buddy" and print it. Any idea how to do it?
I have the following code:
def myExampleFunction = ( text: String ) => {
val result = Http("http://localhost:5001/other/post").postData("{\"my_field\":\"" + text + "\"}")
.header("Content-Type", "application/json")
.header("Charset", "UTF-8")
.option(HttpOptions.readTimeout(10000)).asString
println("result.body is!! : " + result.body)
result.body
Running this prints the following:
result.body is!! : {"your_field": "hello there buddy"}
What I want to achieve is:
result.body is!! : hello there buddy
result.body
is of type string
, parse string data to json data to extract the required field.
In below code I have used json4s
library to parse string response to json data.
def myExampleFunction = ( text: String ) => {
import org.json4s._
import org.json4s.native.JsonMethods._
implicit val formats = DefaultFormats
val result = Http("http://localhost:5001/other/post")
.postData("{\"my_field\":\"" + text + "\"}")
.header("Content-Type", "application/json")
.header("Charset", "UTF-8")
.option(HttpOptions.readTimeout(10000))
.asString
(parse(result.body) \\ "your_field").extract[String]
}