1

I have to convert my json from camelCase to kebab-case.

Example:

My Json:
{
    "disclaimerConfirmed" : true
}

And I need:

{
    "disclaimer-confirmed" : true
}

I cannot use @JsonProperty because it rename this atributes permanently. I am looking for something which will consume Json (can be as String) and returns modified json(as String).

Doctiger
  • 2,318
  • 2
  • 15
  • 28
  • you can also have a look on that (it shows you how to write custom JSON serializers, something that could work for your case), https://stackoverflow.com/questions/12134231/jackson-dynamic-property-names – dZ. Oct 31 '19 at 16:15

2 Answers2

0

You can have different serializers for different cases or you can create pojo with @JsonProperty and use those where-ever required.

For example,

class A {
 private String disclaimerConfirmed;

}

class AkebabCase {
   @JsonProperty("disclaimer-confirmed")
   private String disclaimerConfirmed;
}

So, if you want to serialize to kebab-case you can use converters to convert A to AkebabCase and then serialize.

Vinay Prajapati
  • 7,199
  • 9
  • 45
  • 86
0

Jackson supports naming strategies so you could read the input String to map (with camelCase strategy) and then write the map back to a String (with kebab-case which is natively supported );

Specific method you need to switch these conventions in ObjectMapper without annotations is:

mapper.setPropertyNamingStrategy(PropertyNamingStrategy.*);
drekbour
  • 2,895
  • 18
  • 28