Questions tagged [json4s]

A single AST to be used by other scala json libraries

At this moment there are at least 6 json libraries for scala, not counting the java json libraries. All these libraries have a very similar AST. This project aims to provide a single AST to be used by other scala json libraries.

At this moment the approach taken to working with the AST has been taken from lift-json and the native package is in fact lift-json but outside of the lift project.

https://github.com/json4s/json4s

449 questions
33
votes
1 answer

Extract String value using json4s

I have following Scala console session for json4s, where I am trying to extract String value from a parsed json: scala> import org.json4s._ import org.json4s._ scala> import org.json4s.native.JsonMethods._ import…
tuxdna
  • 8,257
  • 4
  • 43
  • 61
18
votes
2 answers

How can I convert a json string to a scala map?

I have a nested json whose structure is not defined. It can be different each time I run since I am reading from a remote file. I need to convert this json into a map of type Map[String, Any]. I tried to look into json4s and jackson parsers but they…
Udit Mehta
  • 201
  • 1
  • 2
  • 4
16
votes
1 answer

json4s: Convert type to JValue

I have some source object src and would like to get a JValue from it. All the examples and documentation for json4s seem to revolve around getting a JSON-encoded string, like so: def encodeJson(src: AnyRef): String = { import…
Travis Gockel
  • 26,877
  • 14
  • 89
  • 116
16
votes
2 answers

Is it possible to make json4s not to throw exception when required field is missing?

Is it possible to make json4s not to throw exception when required field is missing ? When I "extract" object from raw json string it throws exception like this one org.json4s.package$MappingException: No usable value for pager No usable value for…
expert
  • 29,290
  • 30
  • 110
  • 214
14
votes
8 answers

How to convert Row to json in Spark 2 Scala

Is there a simple way to converting a given Row object to json? Found this about converting a whole Dataframe to json output: Spark Row to JSON But I just want to convert a one Row to json. Here is pseudo code for what I am trying to do. More…
Sami Badawi
  • 977
  • 1
  • 10
  • 22
14
votes
2 answers

Map[String,Any] to compact json string using json4s

I am currently extracting some metrics from different data sources and storing them in a map of type Map[String,Any] where the key corresponds to the metric name and the value corresponds to the metric value. I need this to be more or less generic,…
jarandaf
  • 4,297
  • 6
  • 38
  • 67
14
votes
1 answer

How to serialize object to AST using json4s?

I am writing a Customer Serializer. In that Serializer I would like to somehow say: "and this thing you already know how to serialize". My current approach looks like that: import org.json4s.native.Serialization._ import…
mjaskowski
  • 1,479
  • 1
  • 12
  • 16
13
votes
1 answer

Extracting polymorphic types in json4s

I am using json4s to work with JSON objects in my Scala code. I want to convert JSON data to an internal representation. The following learning test illustrates my problem: "Polimorphic deserailization" should "be possible" in { import…
Kulu Limpa
  • 3,501
  • 1
  • 21
  • 31
12
votes
2 answers

Combining type and field serializers

Let's assume I have a case class with the following setup: case class Place(id:java.util.UUID, name:String) I can write a (working!) serializer for this type as follows: class placeSerializer extends CustomSerializer[Place]( format => ( { …
DeaconDesperado
  • 9,977
  • 9
  • 47
  • 77
11
votes
2 answers

Spark non-serializable exception when parsing JSON with json4s

I've run into an issue with attempting to parse json in my spark job. I'm using spark 1.1.0, json4s, and the Cassandra Spark Connector. The exception thrown is: java.io.NotSerializableException: org.json4s.DefaultFormats Examining the…
worker1138
  • 2,071
  • 5
  • 29
  • 36
11
votes
3 answers

What `JObject(rec) <- someJArray` means inside for-comprehension

I'm learning Json4s library. I have a json fragment like this: { "records":[ { "name":"John Derp", "address":"Jem Street 21" }, { "name":"Scala Jo", "address":"in my sweet…
null
  • 8,669
  • 16
  • 68
  • 98
11
votes
1 answer

Purpose of `render` in json4s

In json4s examples and documentation I often see the idioms compact(render(jval)) and pretty(render(jval)) I do not think I have actually seen an example with compact or pretty applied directly to a code generated JValue, but it is not clear to…
Daniel Mahler
  • 7,653
  • 5
  • 51
  • 90
10
votes
1 answer

How to serialize JSON with json4s with UTF-8 characters?

I have a really simple example: import org.json4s._ import org.json4s.native.JsonMethods._ import org.json4s.JsonDSL._ val json = ("english" -> JString("serialization")) ~ ("japanese" -> JString("シリアライゼーション")) println(pretty(render(json))) What I…
user107057
  • 121
  • 7
10
votes
1 answer

How to parse and extract information from json array using json4s

I am currently trying to extract the information from a json array using json4s (scala). An example data is as follows: val json = """ [ {"name": "Foo", "emails": ["Foo@gmail.com", "foo2@gmail.com"]}, {"name": "Bar", "emails":…
yxjiang
  • 240
  • 1
  • 4
  • 8
10
votes
2 answers

Is there an out of the box (de)serializer for UUID in json4s?

I'm having a problem very similar to what's described here: Combining type and field serializers case class(id: Option[UUID], otherValue:String, … ) The answer to the related question would allow me to write my own format, but I'm curious if there…
iwein
  • 25,788
  • 10
  • 70
  • 111
1
2 3
29 30