score:29
Accepted answer
You can "decompose" a case class into JSON and then render it. Example:
scala> import net.liftweb.json.JsonAST._
scala> import net.liftweb.json.Extraction._
scala> import net.liftweb.json.Printer._
scala> implicit val formats = net.liftweb.json.DefaultFormats
scala> case class MyBean(name: String, age: Int)
scala> pretty(render(decompose(MyBean("joe", 35))))
res0: String =
{
"name":"joe",
"age":35
}
But sometimes it is easier to use DSL syntax:
scala> import net.liftweb.json.JsonDSL._
scala> val json = ("name" -> "joe") ~ ("age" -> 35)
scala> pretty(render(json))
res1: String =
{
"name":"joe",
"age":35
}
Source: stackoverflow.com
Related Query
- Serializing a scala object into a JSon String using lift-json
- Converting a nested scala object into a JSON string using Play JSON
- NotXContentException when serializing Scala object to Json and indexing into Elasticsearch
- Not able to convert json into object using json4s scala
- Scala object to json string using lift-json
- Using scala play-json 2.4.x, how do I extract the name of a json object into a different object?
- How can I construct and parse a JSON string in Scala / Lift
- Converting JSON string to a JSON object in Scala
- Scala : Way to use a function directly into the println(...) using string interpolation
- Print json string in one line using circe in scala
- How to add a json object in to a json array using scala play?
- Parsing a Json String in Scala using Play framework
- Deserializing Json array into Scala object
- Get a specific parameter from a json string using JsonPath in scala
- Turning a string into a sealed trait using scala chimney
- convert scala object to json using json4s
- Flatten any nested json string and convert to dataframe using spark scala
- Add a Json object to JSON Array using scala play
- Unable to deserialize json array to class object using jackson fasterxml in scala
- How to parse a JSON object (which is a list) into simple Scala class objects in scala?
- Extracting string from JSON using Json4s using Scala
- Cannot created object walking though JSON string using Circe JSON parser
- how to set value of attribute in a class and convert it into json using lift json
- Scala object to Json Formatter using combinators
- Scala Lift - Mongodb save data as json object
- How to read a JSON file into a Map, using Scala
- Convert data frame into String using scala and save the ouput to a csv
- Using Sub-Types And Return Types in Scala to Process a Generic Object Into a Specific One
- How to "reads" into a Scala Case Class given a Json object with key names that start with a capital letter
- Seralizing a scala object into a JSON at compile time
More Query from same tag
- org.apache.spark.SparkException: Task not serializable -- Scala
- Bucketed Sink in scalaz-stream
- Weird Behavior of Scala Future and Thread.sleep
- collect on hadoopFile RDD returns duplicate results
- How do you deal with futures and mapAsync in Akka Flow?
- How to enforce creating new transaction in Lift Mapper?
- How to disable Scala assertions in Eclipse?
- Shapeless. How filter LabelledGeneric record by keys?
- Create a list of scenarios and pass to setup
- Scala generic function for dividing a collection by modulo
- Parsing nested JSON values with Lift-JSON
- How to discover the library that defines a class from sbt?
- Akka-Streams: At-Least-Once-Delivery behaviour with Kafka and Cassandra
- How to transform a Scala application into a single-instance persistant HTTP service?
- Custom datasource plugin on EMR throwing java.lang.NoClassDefFoundError: scalaj/http/Http
- PartialFunction that needs access to an outer val
- Exception when running Spark job server in spark standalone mode
- Finagle Thrift NoSuchMethodError
- Derived Orderings in scala?
- Is it possible to access anonymous outer classes?
- Spark unit test fails due to stage failure
- Apache Spark; Graph X ; How to combine two edge RDD
- Is it possible to convert synchronous function call with output of Either[Throwable, Unit] to Future[Either[Throwable, Unit]]?
- Disabling play.jobs.Job from running while in test mode in play framework
- How to use SQL "LIKE" operator in SLICK
- parse Json String using scala.util.parsing.json
- Using Scala's Runtime
- How to suppress automatic conversion from Long to Double when creating an array containing type AnyVal
- How to specify a dynamic input and output path for Spark - in a typesafe config file?
- Maven/Scala: Filename is too long