I'm trying to map directly the output of a Jsonpath to a list of POJO. I'm using Jackson as a mapping provider.
Jsonpath output:
{
"actions" : [
{
"parameterDefinitions" : [
{
"defaultParameterValue" : {
"name" : "PARAM1",
"value" : ""
},
"description" : "Type String",
"name" : "PARAM1",
"type" : "StringParameterDefinition"
},
{
"defaultParameterValue" : {
"name" : "PARAM3",
"value" : ""
},
"description" : "Type String",
"name" : "PARAM3",
"type" : "StringParameterDefinition"
}
]
}
]
}
JobParameter.java (the POJO in which I'd like to map):
public class JobParameter {
private String description;
private String name;
private String type;
// public getters & setters
Jsonpath initialization:
Configuration conf = Configuration
.builder()
.mappingProvider(new JacksonMappingProvider())
.build();
List<JobParameter> jobParameters = JsonPath
.using(conf)
.parse(jsonpathOutput)
.read("$.actions[0].parameterDefinitions[0:]", List.class);
Using the above code, I always get a Map. See below the result of a toString() on this map:
[{defaultParameterValue={name=PARAM1, value=}, description=Type String, name=PARAM1, type=StringParameterDefinition}, {defaultParameterValue={name=PARAM3, value=}, description=Type String, name=PARAM3, type=StringParameterDefinition}]
Note that when I try to map the Jsonpath output to a single object, the deserialization works fine:
Configuration conf = Configuration
.builder()
.mappingProvider(new JacksonMappingProvider())
.build();
JobParameter singleJobParameter = JsonPath
.using(conf)
.parse(jsonpathOutput)
.read("$.actions[0].parameterDefinitions[0]", JobParameter .class);
In the example above, the singleJobParameter instance is well created and filled.
Am I missing something? Thanks!
You must use a TypeRef for this. In your case you must also use the @JsonIgnoreProperties annotation.
Configuration conf = Configuration
.builder()
.mappingProvider(new JacksonMappingProvider())
.jsonProvider(new JacksonJsonProvider())
.build();
TypeRef<List<JobParameter>> type = new TypeRef<List<JobParameter>>(){};
List<JobParameter> jobParameters = JsonPath
.using(conf)
.parse(json)
.read("$.actions[0].parameterDefinitions[0:]", type);
Note that this is not supported by all JsonMappingProviders.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With