1

example json

{
"11var":"value1",
"11var2":"val2",
"11var3":"val3",
"11var4":"val444",
"11var5":"val5",
.....
}

how to convert this to a pojo in latest spring boot and jackson setup?

PS: I know we can do @JsonProperty("11var") and so on for all variables. my point what are the other ways. and also main issue here we cant start variable names with numbers in java check here

ozkanpakdil
  • 3,199
  • 31
  • 48
  • If you already know of *a* solution and you want to find alternatives, then it usually helps to explain **why** you want alternatives. What disadvantage does the known-working solution have that you want to avoid? – Joachim Sauer Nov 15 '22 at 18:22
  • disadvantage is writing @JsonProperty("11var") @JsonProperty("11var2") @JsonProperty("11var3") think we have 60 variables like this, it will be so many fields and so many jsonproperty, maybe @customJsonProperty or any other way around to make it more easy to read and work with. – ozkanpakdil Nov 15 '22 at 18:28
  • @ozkanpakdil use websites like https://www.jsonschema2pojo.org/ to generate those classes. – muhammed ozbilici Nov 15 '22 at 21:26

2 Answers2

1

Use map and jackson objectMapper

String json = "{\"11var\":\"value1\",\"11var2\":\"val2\",\"11var3\":\"val3\",\"11var4\":\"val444\",\"11var5\":\"val5\"}";

ObjectMapper objectMapper = new ObjectMapper();

Map<String, Object> map = objectMapper.readValue(json, Map.class);
System.out.println(map.get("11var"));
dev_in_progress
  • 2,484
  • 2
  • 21
  • 32
0

You can use Jackson Custom Serialization/Deserialization

And register it to the class level

@JsonSerialize(using = ItemSerializer.class)
public class Item {
    ...
}
so-random-dude
  • 15,277
  • 10
  • 68
  • 113