Categories: java, json, kotlin, jackson

How to use dot navigation in Jackson JsonNode

1 answer

I have the following code:

 private val parsed = ObjectMapper().readTree(vcap)  parsed.get("spaces")?.firstOrNull()?.get("block1")?.asText() 

I'd like to use dot notation for navigating (for readibility reasons). Something like:

 private val parsed = ObjectMapper().readTree(vcap)  parsed.get("spaces[0].block1")?.asText() 

Is it possible?

Received answer to this question:
The best answer according to the author of the question:

If you are using jackson greater than 2.3 then you can simply use JsonPointer expression

parsed.at("/spaces/0/block1")?.asText() 

Of if you wanna use dot navigation which is called json pathing you can use Jayway JsonPath

ReadContext ctx = JsonPath.parse(vcap); ctx.read("$.spaces[0].block1");