Search code examples
scalajson4s

Create empty object in JSON via Json4s DSL


The JSON documents I'm creating need to have a properties field, even if there are no properties.

{"foo":"bar","properties":{}} is legal, {"foo":"bar"} is illegal.

How can I define an empty object to use as the properties value using the Json4s DSL?

("foo" -> "bar") ~ ("properties" -> ???)

I have tried Map.empty, new Object, (). All of these are not the correct type.


Solution

  • I never used Json4s, but it seemed to work with:

    val map = ("foo" -> "bar") ~ ("properties" -> Nil)
    

    Here is my full code:

      import org.json4s._
      import org.json4s.native.JsonMethods._
      import org.json4s.JsonDSL._
    
      val map = ("foo" -> "bar") ~ ("properties" -> Nil)
    
      println(compact(render(map)))