shaheer01
shaheer01

Reputation: 121

Conversion from JSON to POJO having sub JSON in java

I am trying to convert from JSON event to a POJO and by keeping sub JSON as it is in the POJO but getting null.

My POJO is this

private String accountId;
private String clientId;
private String datacenter;
private EventType eventType;
private String eventId;
private Long eventRaisedTimeMillis;
private EntityType entityType;
private String entityId;
private String alertName;
private String feature;
private String alertMetadata;
private String alertCondition;
private Map<String, String> attributeValues;

And my event is producing is this:

{
  "clientId": "abc",
  "accountId": "account1",
  "eventType": "STRESS",
  "datacenter": "v",
  "eventRaisedTimeMillis": 1627302648497,
  "entityId": "xyz",
  "entityType": "QUEUE",
  "eventId": "UUID",
  "alertName": "QueueAWT",
  "feature": "Fallback",
  "alertMetaData": {
    "value": {
      "queues": [
        "Q1",
        "Q2"
      ]
    }
  },
  "alertCondition": "JSON",
  "attribute_values": {
    "attr1": "value1",
    "attr2": "value2"
  }
}

But I am getting this as output on consuming from kafka:

(accountId=accounnt1, clientId=abc, eventType=STRESS, eventId=UUID, eventRaisedTimeMillis=1627302648497, entityType=QUEUE, entityId=xyz, alertName=QueueAWT, feature=Fallback, alertMetadata=null, alertCondition=JSON, attributeValues=null)

One way is to create a class for alertMetadata but do not want to do that but rather store it as sub JSON inside it.

Upvotes: 0

Views: 94

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191983

Unclear what JSON library you are using, but your POJO has no annotations. So, your JSON has alertMetaData, and the POJO has alertMetadata... These should match. Also note attribute_values is null for attributeValues as well. With Jackson, you can use @JsonProperty to fix this

For example

@JsonProperty(value = "alertMetaData")
private String alertMetadata;

@JsonProperty(value = "attribute_values")
private Map<String, String> attributeValues;

You're also producing an Object for the metadata, but the POJO has just a string. If you don't want to create a nested class, then use a Map<String, Object>, similarly to how you did for the attributeValues

Upvotes: 1

Padua
Padua

Reputation: 69

Assuming that you are getting the json from the web, I recommend you to use @RequestBody to get the JSON data and then using Mapstruct to map the attributes from JSON to your POJO

toPOJO(@RequestBody JsonDTO jsonDTO)

then you can use mapstruct like this, to map everything:

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "spring")
public interface PojoMapper {

    @Mapping(source = "attribute_values", target = "attributeValues")
    Pojo toEntity(JsonDTO jsonDTO);

}

keep in mind you can use as many @Mapping annotations as you need if your Json attributes have different names from your pojo attributes...

a mapperImpl file will be automatically created when you run maven clean install, mapping everything that came from the json to your pojo.

Upvotes: 0

Related Questions