Return part of JSON into object Spring boot

Viewed 83

I have JSON structure that goes like this

{
    "name":"John",
    "age":27,
    "company":{
        "company_name":"ACME LLC",
        "address": "1st Street",
        "country": "US"
    }
}

I know that if I want to map this to java object I need some kind of mapper and I have done that.

My question is: Is there a way to map only part of this json into object. I would like to map only company part of json into object. One more thing, I'm using java 11 with spring boot and I have access to Jackson's Object mapper.

Edit: Also if there is a way to navigate to this object it would be ok. For example $.company

3 Answers

I think the easiest way is to create two model classes. First with one field:

@Data
public class Model {

private Company company;

}

and second:

@Data
public class Company {

  @JsonProperty("company_name")
  private String companyName;
  
  private String address;
  
  private String country;

}

Then you can use Jackson mapper. You don't need to mention "map" and "age" fields in the Model class.

Ok, I have found an answer, thank you @Jakub on your answer it was a great help. Link to comment.

This is code example of answer based on my question:

public class Company {

    private String name;
    private String address;
    private String country;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

Test method/execution:

   @Test
    public void testJSON() throws JsonProcessingException {
        JsonNode productNode = objectMapper.readTree("{\n" +
                "    \"name\":\"John\",\n" +
                "    \"age\":27,\n" +
                "    \"company\":{\n" +
                "        \"name\":\"ACME LLC\",\n" +
                "        \"address\": \"1st Street\",\n" +
                "        \"country\": \"US\"\n" +
                "    }\n" +
                "}");

        Company company = objectMapper.readValue(productNode.get("company").toString(), Company.class);

        System.out.println(company.getAddress());
    }

I would suggest using DSM library.

define your mapping in yaml file, you only want to company field. So your mapping file will be like below.

result:
  type: object
  path: /company
  fields:
    name:  # read value from field in path 
       path: company_name 
    address:  #field name is the same
    country:

Java code to read file:

DSM dsm=new DSMBuilder(new File("path/to/mapping.yaml")).create();
Map<String,Object> result=  (    Map<String,Object>)dsm.toObject(jsonData);

result is a map that contains information in company field in json.

If you want to directly get instance of the class by deserialization:

public class Company{
   String name;
   String address;
   String country;
}

Java code to deserialize:

DSM dsm=new DSMBuilder(new File("path/to/mapping.yaml")).create();
Company result= dsm.toObject(jsonData,Company.class);
Related