0

I have a nested JSON that I am reading data from. I would like to take only one specific field and display it in the console.

To map my JSON in object model I created 3 classes CarResponse, Car and CarValue.

CarResponse.java

public class CarResponse {

    List<Car> car = new ArrayList<Car>();

    public List<Car> getCar() {
        return car;
    }

    public void setCar(List<Car> car) {
        this.car = car;
    }
   
    //Getters and Setters

   @Override
public String toString() {

    String str = "=================================\r\n";
    // Start of the day
    ZonedDateTime zdt = LocalDate.now().atStartOfDay(ZoneId.systemDefault());
    StringBuilder sb = new StringBuilder();
    sb.append(zdt.toString()).append(System.lineSeparator());
    for (int i = 1; i <= 23; i++) {
        zdt = zdt.plusHours(1);
        sb.append(zdt.toString()).append(System.lineSeparator());
        for (Car ld : car) {
            str += "\t" + "Shop: " + ld.getShop() + "\r\n";
            str += "\t" + "Date: " + ld.getDate() + "\r\n";
            str += "\t" + "Values: " + ld.getValues() + "\r\n";
        }
        System.out.println(sb);
        return str;
    }
    return null;
}

Car.java

public class Car {

    private String shop;
    private String date;
    @JsonDeserialize(using = CustomDeserializer.class)
    private CarValues values;

    //Getters and Settrs  
 
    @Override
    public String toString() {
        String str = "=================================\r\n";
        str += "Shop: " + shop + "\r\n" +
                "Date: " + date + "\r\n";

        for(CarValue ld : values) {
            str += "\t" + "Name: " + ld.getName()+ "\r\n";
            str += "\t" + "Age: " + ld.getAge() + "\r\n";
            str += "\t" + "Country: " + ld.getCountry() + "\r\n";
        }
        return str;
    }
}

CarValue.java

public class CarValue {
    private String name;
    private String country;
    private Long age;
    
    //Getters and Setters and toString

I created custom deserializer to get name field/key from JSON.

CustomDeserializer.java

public class CustomDeserializer extends JsonDeserializer {

    @Override
    public QuoteValue deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        ObjectCodec oc = p.getCodec();
        JsonNode node = oc.readTree(p);
        QuoteValue value =  new QuoteValue();
        value.setname(node.toString());
        return value;
    }

}

data.json

{
  "car": [
    {
      "shop": "Audi Germany",
      "date": 1573599600000,
      "values": [
        {
          "name": "Audi Xl",
          "age": "2020",
          "country": "Germany"
        },
        {
          "name": "Audi i",
          "age": "2021",
          "country": "France"
        },
        {
          "name": "Bmw Xl",
          "age": "2020",
          "country": "Spain"
        },
        {
          "name": "Citroen",
          "age": "1990",
          "country": "France"
        }
        ]
    }]
}

Right now this output is shown in console:

2021-02-27T09:00+01:00[Europe/Zagreb]
    Shop: Audi
    Date: 1573599600000
    Values: QuoteValue{tLabel='[{"name":"Audi Xl","age":"2020","country":"Germany"},{"name":"Audi i","age":"2021","country":"France",{"name":"Bmw","age":"2020","country":"Spain"},{"name":"Citroen","age":"1990","country":"France",}]}

And I would like to display

2021-02-27T09:00+01:00[Europe/Zagreb]
        Date: 1573599600000
        Values:[{"name":"Audi Xl"},{"name":"Audi i"},{"name":"Bmw"},{"name":"Citroen"}]}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
user9347049
  • 1,927
  • 3
  • 27
  • 66

2 Answers2

0

You can annotate the serializable classes with the

    @JsonIgnoreProperties(value = { "intValue" })

where you can add multiple fields to be ignored in serialization.

No need to write extra code :)

You can refer to the full explaination here

https://www.baeldung.com/jackson-ignore-properties-on-serialization

0

Maybe if you modify some classes like this :

Car: Changing values to a List (you gave no information about any classes named CarValues or QuoteValue) and removing unnecessary toString

public class Car {

    private String shop;
    private String date;
    private List<CarValue> values;  // <- a list
}

CarValue : adding a toString override:

public class CarValue {
    private String name;
    private String country;
    private Long age;

    @Override
    public String toString() {
        return "{\"name\": \"" + name + "\"}";
    }
}

With this setup, it should works more or less as expected, and you don't need a Custom Deserializer. assuming you read your json like this :

CarResponse carResponse = objectMapper.readValue(json, CarResponse.class)

If you still do need a JsonDeserializer, please use the parameterized type, like :

public class CustomQuoteDeserializer extends JsonDeserializer<QuoteValue> {
jmbourdaret
  • 211
  • 1
  • 6