Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Serialize HashMap as root element

Tags:

java

json

jackson

I have a class which is basically a wrapper for a HashMap. I would like to serialize this class with Jackson to an JSON object without any wrapping element.

   public class Customers {

        @JsonProperty
        private Map<String,Customer> customers = new HashMap<>();

        ...     

    }

Current serialization looks like this:

{
   "Customers":{
       "customers":{
          "keyX":{...},
          "keyY":{...},
          "keyZ":{...}
}

But I want to have this:

{
   "keyX":{...},
   "keyY":{...},
   "keyZ":{...}
}

How can I reach it?

like image 653
Marko Avatar asked Apr 15 '26 02:04

Marko


1 Answers

Try @JsonUnwrapped on the property Customers this can help you unwrap a nested level

like image 125
Filippo Fratoni Avatar answered Apr 17 '26 16:04

Filippo Fratoni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!