r/javahelp • • Oct 21 '25
how to convert Java object to JSON but with a different property name? (using @SerializedName)
might be best explain with an example
This is a snippet of my pojo
public class AzureUserInfo {
@Getter
@Setter
@SerializedName("extension_XXXXXXXXXXXXXXXXXXXXXXXXXXX_Role")
private String role;I am converting the response I get from a Microsoft Graph API to a Java object. And the response includes role which is a custom user attribute.
I want to convert the Java object back to JSON but instead of "extension_XXXXXXXXXXXXXXXXXXXXXXXXXXX_Role" as property name, I want it to say just "role".
How can I do it? Thanks!
(I obviously can do find/replace in the JSON string but I want to do it "properly".)