Jackson: remove some values from json and keep some null values

Seyed Ali Roshan

I have a model like this:

public class Employee {
    @JsonProperty("emplyee_id")
    private Integer id;
    @JsonProperty("emplyee_first_name")
    private String firstName;
    @JsonProperty("emplyee_last_name")
    private String lastName;
    @JsonProperty("emplyee_address")
    private String address;
    @JsonProperty("emplyee_age")
    private Byte age;
    @JsonProperty("emplyee_level")
    private Byte level;

    //getters and setters
}

now I need to create two JSONs using this (only) model.

the first one must like this for example:

{
    "employee_id":101,
    "employee_first_name":"Alex",
    "employee_last_name":"Light",
    "employee_age":null,
    "employee_address":null
}

and the second one must like this for example:

{
    "employee_id":101,
    "employee_level":5
}

by the way, I already tested @JsonIgnore and @JsonInclude(JsonInclude.Include.NON_NULL).

the problem of the first one (as much as I know) is, those fields can't be included in other JSONs (for example if level get this annotation, it won't be included in the second JSON)

and the problem of the second one is, null values can't be included in JSON.

so can I keep null values and prevent some other property to be included in JSON without creating extra models? if the answer is yes, so how can I do it? if it's not I really appreciate if anyone gives me the best solution for this state.

thanks very much.

Kalaiselvan

it could be useful for you using @JsonView annotation

public class Views {
    public static class Public {
    }
    public static class Base {
    }
 }



public class Employee {
   @JsonProperty("emplyee_id")
   @JsonView({View.Public.class,View.Base.class})
   private Integer id;

   @JsonProperty("emplyee_first_name")
   @JsonView(View.Public.class)
   private String firstName;

   @JsonProperty("emplyee_last_name")
   @JsonView(View.Public.class)
   private String lastName;

   @JsonProperty("emplyee_address")
   private String address;

   @JsonProperty("emplyee_age")
   private Byte age;

   @JsonProperty("emplyee_level")
   @JsonView(View.Base.class)
   private Byte level;

   //getters and setters
 }

in your json response add @JsonView(Public/Base.class) it will return based on jsonview annotations

//requestmapping
@JsonView(View.Public.class)  
public ResponseEntity<Employee> getEmployeeWithPublicView(){
    //do something
}

response:

{ 
  "employee_id":101,
  "employee_first_name":"Alex",
  "employee_last_name":"Light",
  "employee_age":null,
  "employee_address":null
}

for the second one

//requestmapping
@JsonView(View.Base.class)  
public ResponseEntity<Employee> getEmployeeWithBaseView(){
    //do something
}

response

{
   "employee_id":101,
   "employee_level":5
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to remove some values from a python dictionary

Remove duplicates from array, preserving some values

Remove prefix from some values in a col in dataframe

JSON not deserializing some values

Getting some values from JSON response

Extracting some values from JSON objects in AngularJS

How can I remove some keys from a JSON string where the values are preventing me from parsing it as JSON?

How to remove the whitespace while concatenating while some have Null values?

Concatenate results with some null values

How to remove the null values from the json input

Remove null values from json file javascript

Postgres - Remove NULL values from a json array

why is spring boot returning me a json with null for some values?

Remove some values from collection of collections using java streams

How to remove some values from a 4-dimensional matrix?

Get some values in json array

Create dataframe from objects with some NULL values in R

replacing null values from in map with some other value

How to return null in SUM if some values are null?

Remove duplicates from list but only keep some

How to get some values from a JSON string in C#?

Iterate and get unique values from JSON for some indexes

Seperate some values from json object (react , jquery)

Create new JSON file from another with some changed values in PHP

Excel Remove duplicate values that has some spaces

Remove duplicate values in pandas but after some manipulation

How to write JSONata query to remove duplicate values from some tags in json and to get unique value of each object using jsaonata

pyspark replacing null values with some calculation related to last not null values

MySQL - selecting rows with some values being null