skip to Main Content

I use the following to convert a JSON into an Object:

JSON:

{
     "color": "blue"
}

Code:

somethingDto = objectMapper.readValue(myJson, SomethingDTO.class);

However, let’s say that the JSON comes at the following format:

{
    "something": {
         "color": "blue"
    }
}

What code can achieve the same result?

Edit: There may be more properties alongside "something"

2

Answers


  1. You should create a root class for it for example

    public class RootDto {
         SomethingDTO somthing;
    
         public SomethingDTO getSomething() {
          return somthing;
         }
        
        
         public void setSomething(SomethingDTO somthing) {
          this.somthing = somthing;
         }
    }
    

    Then map your JSON to it

    RootDto rootDto = objectMapper.readValue(myJson, RootDto.class);
    

    Or you can use JsonNode

    JsonNode rootNode = objectMapper.readTree(myJson);
    JsonNode somethingNode = rootNode.get("something");
    SomethingDTO somethingDto = objectMapper.convertValue(somethingNode, SomethingDTO.class);
    

    Also, you can handle both of them in this way

    JsonNode rootNode = objectMapper.readTree(myJson);
    JsonNode somethingNode = rootNode.get("something");
    SomethingDTO somethingDto;
    if (somethingNode != null) {
        somethingDto = objectMapper.convertValue(somethingNode, SomethingDTO.class);
    } else {
        somethingDto = objectMapper.convertValue(rootNode, SomethingDTO.class);
    }
    
    Login or Signup to reply.
  2. Threre is an annotation called @JsonRootName which is similar to @XmlRootElement. That annotation indicates the name of the "root" wrapping.

    @JsonRootName("something")
    public class SomethingDto {
    
        String color;
    
        public String getColor() {
            return color;
        }
    
        public SomethingDto setColor(String color) {
            this.color = color;
            return this;
        }
    }
    

    After that, the fearure must be enabled:

    ObjectMapper om = new ObjectMapper()
            .enable(SerializationFeature.WRAP_ROOT_VALUE)
            .enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
    
    

    Now, it will wrap / unwrap objects to desired root element(s).

    class WrapTest {
    
        private static final ObjectMapper om = new ObjectMapper()
                .enable(SerializationFeature.WRAP_ROOT_VALUE)
                .enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
    
        //language=json
        private static final String input = """
                {
                    "something": {
                        "color" : "blue"
                    }
                }
                """;
    
        @Test
        void wrappedTest() throws JsonProcessingException {
            var dto = om.readValue(input, SomethingDto.class);
            Assertions.assertAll(
                    () -> assertNotNull(dto),
                    () -> assertEquals("blue", dto.getColor(), "color field mismatch")
            );
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search