0

I am trying to get date value from JsonNode. I have a car object which has car name and released LocalDate.

public class Car {

  String carName;

  LocalDate released;

}

I need to convert it into JsonNode, so I am doing this way.

  ObjectMapper obj = new ObjectMapper();

  JsonNode node = obj.valueToTree(car);

Until here I am good. Once I have JsonNode I need to read it from it.

String carName = node.at("/carName").textValue();

LocalDate date = node.at("/released").textValue(); 

How can I get LocalDate value in LocalDate format or String format from JsonNode?

  • 1
    Could you clarify a bit? If you want the text representation of the date, won't `String date = node.at("/released").textValue(); ` work then? I'm struggling to understand what you are trying to accomplish when you go from a concrete class to JsonNode and then try to read out the values. – jokarls Oct 22 '20 at 15:55
  • Hi, thanks for looking into it. actually, I am trying this way, String date = node.at("/released").textValue(); but it is not working and unfortunately there is no method available like asDateValue(). So answer to your question, I need to get LocalDate value from JsonNode. it doesn't matter how and what I use. Hope this makes sense.Thanks again! – CuriousBrain Oct 22 '20 at 16:00
  • Ok so what happens when you call `.textValue()`? – jokarls Oct 22 '20 at 16:05
  • I get null. for string value let's say I want to get carName, I can do .textValue() and able to get the string value. – CuriousBrain Oct 22 '20 at 16:10
  • Yeah, it’s probably not a string value. Check this post for options on how to change the serialization of a LocalDate https://stackoverflow.com/questions/28802544/java-8-localdate-jackson-format – jokarls Oct 22 '20 at 16:26
  • Thanks, it worked. I used objectMapper.registerModule(new JavaTimeModule()); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); I am able to get string value from JsonNode now. – CuriousBrain Oct 22 '20 at 16:58

1 Answers1

0

it worked. I used objectMapper.registerModule(new JavaTimeModule()); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); I am able to get string value from JsonNode now.