score:1
You'd have to use pattern matching/extractors:
val aOpt: List[String] = for {
JObject(map) <- parse(body)
JField("a", JString(value)) <- map
} yield value
alternatively use querying DSL
parse(body) \ "a" match {
case JString(value) => Some(value)
case _ => None
}
These are options as you have no guarantee that arbitrary JSON would contain field "a"
.
See documentation
extract
would make sense if you were extracting whole JObject
into a case class
.
score:3
To use extract
you need to create a class that matches the shape of the JSON that you are parsing. Here is an example using your input data:
val body ="""
{
"a": "hello",
"b": "goodbye"
}
"""
case class Body(a: String, b: String)
import org.json4s._
import org.json4s.jackson.JsonMethods._
implicit val formats = DefaultFormats
val b = Extraction.extract[Body](parse(body))
println(b.a) // hello
Source: stackoverflow.com
Related Query
- Extracting string from JSON using Json4s using Scala
- extracting keys from json string using json4s
- Extracting a field from a Json string using jackson mapper in Scala
- Get a specific parameter from a json string using JsonPath in scala
- Using forall() in extracting String from Option[String] in scala
- Generating Json string using Json4S from a list containing Some and None values
- How can i create a dataframe from a complex JSON in string format using Spark scala
- Reading value from JSON string Scala using ScalaJson
- How to read list of string from JSON file using Scala
- Serializing a scala object into a JSon String using lift-json
- Map[String,Any] to compact json string using json4s
- How to parse and extract information from json array using json4s
- Print json string in one line using circe in scala
- How to read json data using scala from kafka topic in apache spark
- How can I deserialize from JSON with Scala using *non-case* classes?
- Re-using A Schema from JSON within a Spark DataFrame using Scala
- Parsing a Json String in Scala using Play framework
- How to read a json response from a akka-http response entity using json4s
- Producing json in a Scala app using json4s
- Parsing string in json to java.time.LocalTime using json4s
- convert scala object to json using json4s
- Flatten any nested json string and convert to dataframe using spark scala
- How to extract json from a jsonp string in Scala
- Manipulating array in JSON using Scala and JSON4S
- scala code functional-programming facelift: extracting String from List[List[myClass.toString]] structure
- Schema conversion from String to Array[Structype] using Spark Scala
- Fetch all values irrespective of keys from a column of JSON type in a Spark dataframe using Spark with scala
- Not extracting json properly using json4s
- Extracting Specific Field from String in Scala
- Extract a Path from a server log String using regular expressions in Scala
More Query from same tag
- Scala - IntelliJ IDEA Error: Could not find or load main class
- Release IO resources in scala without maintaining mutable state
- Play! 2.0 framework multi Module project
- Spark: subtract dataframes but preserve duplicate values
- Json4s: Decomposing to JValue gives ArrayIndexOutOfBoundsException
- Running Spark Application from Eclipse
- OneHotEncoder in Spark Dataframe in Pipeline
- Addition with generic type parameter in Scala
- How to make app resources accessible to sbt console initialCommands?
- Stackable trait/decorator and abstract class
- How to do something like this in Scala?
- spark, scala & jdbc - how to limit number of records
- function param issue in Scala 11, works in Scala 12 using resilience4j
- What is the reason behind the `=>` in a self type?
- Spark: Why the StructType merge method is private?
- Why was getMonth deprecated on java.sql.Date and java.util.Date
- Error importing object in scala
- Scala initialize empty stacks
- QueryString parsing in Play
- Correct usage of scalaz Future for async execution
- Scala: Declare a variable for holding a function (lambda) without immediate assignment
- Sum of Values based on key in scala
- Sum values of each unique key in Apache Spark RDD
- Scala - JSON object is polymorphic on a field type
- Weird Behavior of Scala Future and Thread.sleep
- Play Framework SBT import play.api.libs.streams
- How to implement HDRF algorithm?
- When I run my test suites they fail with PSQLException: FATAL: sorry, too many clients already
- Scala case syntax understanding
- How to convert Scala JsArray to custorm object