Map Jsonpath output to POJO list

I am trying to directly map Jsonpath output to a POJO list. I use 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 (POJO in which I would like to display):

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 code above, I always get a card. Below is the result of 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, 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 above example, an instance of singleJobParameter is well created and populated.

Am I missing something? Thank!

+4
source share
3 answers

POJO .

@JsonIgnoreProperties(ignoreUnknown = true)
public class JobParametersWrapper {

   private List<JobParameter> parameterDefinitions;

   public List<JobParameter> getParameterDefinitions() {
      return parameterDefinitions;
   }

   public void setParameterDefinitions(List<JobParameter> parameterDefinitions) {
      this.parameterDefinitions = parameterDefinitions;
   }
}
0

TypeRef. @JsonIgnoreProperties.

   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);

, JsonMappingProviders.

+5
<dependency>
    <groupId>com.github.jsurfer</groupId>
    <artifactId>jsurfer-simple</artifactId>
    <version>1.2.2</version>
</dependency>

With JsonSurfer, you can achieve this in two lines:

JsonSurfer jsonSurfer = JsonSurfer.jackson();
Collection<JobParameter> parameters = jsonSurfer.collectAll(json, JobParameter.class, "$.actions[0].parameterDefinitions[*]");

And don't forget to ignore the unused defaultParameterValue parameter in your POJO.

@JsonIgnoreProperties({"defaultParameterValue"})
private static class JobParameter {

    private String description;
    private String name;
    private String type;
0
source

All Articles