1

I am using jackson to convert json to java object, but the problem I am facing is that say the json format is:

{
  employeeId: 123
  name: akash
  ........some other attributes
}

and the java object i am converting it to, has attributes:

id:
name:
.....some other attributes

Now I want to convert that json into the object, you can see that all the attributes have same name, except for employeeid/id. Considering that I can't change the json nor the java object. How can I go about it to do the function.

2 Answers 2

2

Add this on the getter method of id in your model class.

@JsonProperty("employeeId")
Sign up to request clarification or add additional context in comments.

4 Comments

thanks, btw after adding this, will both id and employeeId work?
It will map employeeId from Json to your id in model class. If you have another member variable id in your model class, you dont need to do the above mentioned mapping at all
If there are two variables in your model class, say id and employeeId, Json property will automatically be mapped to employeeId in model class and id will be null
My question was say in some case I want id to be mapped, and in some employee id to be mapped. How to do that? The case required has only one of them present. I am asking if it is possible?
0

You can used @JsonProperty("nameOfProperty") which is used for

Property to serialize (when applied to a "getter" method)
Property to deserialize (when applied to a "setter" method) Field-backed
property to serialize and deserialize (when applied to a non-static member

field)

So your call look like

class abc
{
   @jsonProperty("employeeId")
   int id;  

   //your other class variable and getter-setter methods
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.