carlaodev
carlaodev

Reputation: 641

How to mapper a tree json object using jackson to List<>

After i execute a query, the result is converted to JSON. The format is like:

[
    {
        "version": "1.0.1",
        "device.id": 1234,
        "user.id": 1234,
        "device.platform": "IOS",
        "lastActivity": null,
        "id": 987,
        "when": "2017-08-05",
        "device.platformVersion": "1.2.2",
        "endPointArn": "arn-here-123"
    },
    {
        "version": "1.0.2",
        "device.id": 2345,
        "user.id": 9876,
        "device.platform": "IOS",
        "lastActivity": null,
        "id": 753,
        "when": "2017-08-05",
        "device.platformVersion": "1.2.2",
        "endPointArn": "arn-here-123"
    }
]

I need to mapper this json for a List<> objects where:

public class DeviceUser {
  private Integer id;
  private String version;
  private Date when;
  private String endPointArn;
  private Device device;
  private User user;
}

we can see the keys with attrObject.fieldObject (e.g: "device.id": 2345) and i dont see the way to convert for espected format. The final format spected is:

[
    {
        "version": "1.0.1",
        "user": {
            "id": 1234
        },
        "device": {
            "id": 1234,
            "platform": "IOS",
            "platformVersion": "1.2.2"
        },
        "lastActivity": null,
        "id": 987,
        "when": "2017-08-05",
        "endPointArn": "arn-here-123"
    },
    {
        "user": {
            "id": 9876
        },
        "device": {
            "id": 2345,
            "platform": "IOS",
            "platformVersion": "1.2.2"
        },
        "version": "1.0.2",
        "lastActivity": null,
        "id": 753,
        "when": "2017-08-05",
        "endPointArn": "arn-here-123"
    }
]

Upvotes: 0

Views: 99

Answers (2)

carlaodev
carlaodev

Reputation: 641

So, i resolved this case using a new transformer on result set. The transformer in really just iterate on result and search for ".". Using reflexion. I guess this a poor strategy. And so, i accept new answers.

The transformer that i used is: AliasToBeanNestedResultTransformer

Upvotes: 0

superz600
superz600

Reputation: 54

If you are using Jackson, try to annotate the fields (or the getters/setters) with JsonPropety annotation.

Like this:

@JsonProperty("device.id") private Device device;

Upvotes: 2

Related Questions