Search code examples
javajsonjacksonjackson2

Generate an empty set of Json braces- using java & JSON


How to generate an empty json node using jackson-java. I tried with NullNode.instance, but that returns

"totals":null

Whereas I want totals to be an empty instance.

{
  "totals": {},
  "orderId": "550047004",
  "numberOfItems": 2
}

Solution

  • You should use ObjectNode. It can be created with ObjectMapper:

    ObjectNode node = mapper.createObjectNode();