Abhi Jain
Abhi Jain

Reputation: 73

Parsing a JSON in SpringBoot

I want to parse my JSON data using SpringBoot. However, I am facing some issues in doing so. For example, say my Input JSON is as follows:

{
    "name": "XYZ",
    "gender" : "MALE",
    "Male Item" : {
        "property1": "abc",
        "property2" : "xxx"
    }
}

However, if say, my input have this structure:

{
    "name": "XYZ",
    "gender" : "FEMALE",
    "Female Item" : {
        "property3": "abcd",
        "property4" : "xffxx"
    }
}

If gender is coming as Male, then based on that, user would be sending me different properties than the user whose gender is Female. So, while parsing, if I make a User Class, then how my object mapper will decide which properties to pick?

Upvotes: 0

Views: 1012

Answers (1)

silentsudo
silentsudo

Reputation: 6963

@SerializedName is the key here to solve this. Here, how would I implement it:

Pojo Class UserResponse.java

class UserResponse {
        private String name;
        private String gender;
        @SerializedName("Male Item")
        private Map<String, String> maleProp;
        @SerializedName("Female Item")
        private Map<String, String> femaleProp;

        public String getName() {
            return name;
        }

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

        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }

        public Map<String, String> getMaleProp() {
            return maleProp;
        }

        public void setMaleProp(Map<String, String> maleProp) {
            this.maleProp = maleProp;
        }

        public Map<String, String> getFemaleProp() {
            return femaleProp;
        }

        public void setFemaleProp(Map<String, String> femaleProp) {
            this.femaleProp = femaleProp;
        }

        @Override
        public String toString() {
            return "UserResponse{" +
                    "name='" + name + '\'' +
                    ", gender='" + gender + '\'' +
                    ", male=" + maleProp +
                    ", female=" + femaleProp +
                    '}';
        }
    }

Utils Function to read from stream/string and then converting it to pojo

private static UserResponse jsonToPojo(String fileName) throws FileNotFoundException {
        File file = new File(fileName);
        JsonReader jsonReader = new JsonReader(new FileReader(file));
        Gson gson = new Gson();
        return gson.fromJson(jsonReader, UserResponse.class);
}

This is how would i call, for simplicity i am using main method;

public static void main(String[] args) throws FileNotFoundException {
        System.out.println(jsonToPojo("male.json"));
        System.out.println(jsonToPojo("female.json"));
}

Output would be:

UserResponse{name='XYZ', gender='MALE', male={property1=abc, property2=xxx}, female=null}
UserResponse{name='XYZ', gender='FEMALE', male=null, female={property3=abcd, property4=xffxx}}

Upvotes: 0

Related Questions