What you are trying to do is also called as Xpath in technical terms. It is used for html, xml based languages as well as now available in json

You can try jsonpath for this case:

https://www.baeldung.com/guide-to-jayway-jsonpath https://github.com/json-path/JsonPath

Answer from swapyonubuntu on Stack Overflow
🌐
Makeseleniumeasy
makeseleniumeasy.com › 2020 › 09 › 01 › rest-assured-tutorial-44-fetch-value-from-json-object-using-jsonnode-jackson
REST Assured Tutorial 44 – Fetch Value From JSON Object Using JsonNode – Jackson – get() & path() Methods
We need to use class ObjectMapper provided by Jackson API. ObjectMapper class provides a method “readTree()” which is responsible to deserialize JSON content as tree expressed using a set of JsonNode instances. We can get the value of a node using get() and path() methods of JsonNode class.
🌐
Jenkov
jenkov.com › tutorials › java-json › jackson-jsonnode.html
Jackson JsonNode
This path expression specifies the complete path from the root JsonNode and down to the field you want to access the value of. This is similar to the path to a file from the root of the file system down to the file in a Unix file system.
🌐
Red Hat
access.redhat.com › webassets › avalon › d › red-hat-jboss-enterprise-application-platform › 7.1.beta › javadocs › com › fasterxml › jackson › databind › JsonNode.html
JsonNode (Red Hat JBoss Enterprise Application Platform 7.1.0.Beta1 public API)
Specified by: get in interface TreeNode · Returns: Node that represent value of the specified field, if this node is an object and has value for the specified field. Null otherwise. public abstract JsonNode path(String fieldName) This method is similar to get(String), except that instead of returning null if no such value exists (due to this node not being an object, or object not having value for the specified field), a "missing node" (node that returns true for isMissingNode()) will be returned.
🌐
Makeseleniumeasy
makeseleniumeasy.com › 2020 › 09 › 16 › rest-assured-tutorial-46-fetch-value-from-json-array-using-jsonnode-jackson-get-path-methods
REST Assured Tutorial 46 – Fetch Value From JSON Array Using JsonNode – Jackson – Get() & Path() Methods
We need to use class ObjectMapper provided by Jackson API. ObjectMapper class provides a method “readTree()” which is responsible to deserialize JSON content as tree expressed using a set of JsonNode instances. We can get the value of a node using get() and path() methods of JsonNode class.
🌐
Fasterxml
fasterxml.github.io › jackson-databind › javadoc › 2.7 › com › fasterxml › jackson › databind › JsonNode.html
JsonNode (jackson-databind 2.7.0 API)
Specified by: get in interface TreeNode · Returns: Node that represent value of the specified field, if this node is an object and has value for the specified field. Null otherwise. public abstract JsonNode path(String fieldName) This method is similar to get(String), except that instead of returning null if no such value exists (due to this node not being an object, or object not having value for the specified field), a "missing node" (node that returns true for isMissingNode()) will be returned.
🌐
Tabnine
tabnine.com › home page › code › java › com.fasterxml.jackson.databind.jsonnode
com.fasterxml.jackson.databind.JsonNode.path java code examples | Tabnine
public static CoinbaseMoney getCoinbaseMoneyFromCents(JsonNode node) { final String amount = node.path("cents").asText(); final String currency = node.path("currency_iso").asText(); final int numDecimals = (currency.equalsIgnoreCase("BTC")) ? 8 : 2; return new CoinbaseMoney(currency, new BigDecimal(amount).movePointLeft(numDecimals)); } origin: Graylog2/graylog2-server · private static double timestampValue(final JsonNode json) { final JsonNode value = json.path(Message.FIELD_TIMESTAMP); if (value.isNumber()) { return value.asDouble(-1.0); } else if (value.isTextual()) { try { return Double.p
Find elsewhere
🌐
Java Code Geeks
javacodegeeks.com › home › enterprise java
Find Nested Key via Jackson Example - Java Code Geeks
October 8, 2024 - Line 9: this method will find the nested key based on the given paths. Line 18, 28: utilize the findValue method to find the field from JSON. Line 21: when the field is not found, findValue returns null. In this step, I will create a FindNodeByPath.java that finds a nested node via Jackson JsonNode.path method.
🌐
Makeseleniumeasy
makeseleniumeasy.com › 2020 › 09 › 05 › rest-assured-tutorial-45-fetch-value-from-nested-json-object-using-jsonnode-jackson-at-method
REST Assured Tutorial 45 – Fetch Value From Nested JSON Object Using JsonNode – Jackson – at() Method
September 5, 2020 - We need to use class ObjectMapper provided by Jackson API. ObjectMapper class provides a method “readTree()” which is responsible to deserialize JSON content as tree expressed using a set of JsonNode instances. We can get the value of a node using get() and path() methods of JsonNode class.
🌐
Baeldung
baeldung.com › home › json › jackson › working with tree model nodes in jackson
Working with Tree Model Nodes in Jackson | Baeldung
January 8, 2024 - To verify that the method works as expected, we’ll change the value of the field name under root node from an object of first and last into another one consisting of only nick field in a test: @Test public void givenANode_whenModifyingIt_thenCorrect() throws IOException { String newString = "{\"nick\": \"cowtowncoder\"}"; JsonNode newNode = mapper.readTree(newString); JsonNode rootNode = ExampleStructure.getExampleRoot(); ((ObjectNode) rootNode).set("name", newNode); assertFalse(rootNode.path("name").path("nick").isMissingNode()); assertEquals("cowtowncoder", rootNode.path("name").path("nick").textValue()); }
🌐
Tabnine
tabnine.com › home page › code › java › org.codehaus.jackson.jsonnode
org.codehaus.jackson.JsonNode.path java code examples | Tabnine
private Iterator<JsonNode> dataElements(InputStream dataStream) throws IOException, JsonParseException, JsonMappingException { JsonNode rootNode = OBJECT_MAPPER.readValue(dataStream, JsonNode.class); JsonNode dataNode = rootNode.path(KeyValueTokens.DATA_TOKEN); Iterator<JsonNode> elements = dataNode.getElements(); return elements; } ... private JsonNode valueNode(JsonNode element) { if (element.has(KeyValueTokens.VALUE_TOKEN)) { return element.path(KeyValueTokens.VALUE_TOKEN); } else { throw new IllegalArgumentException("Given dataset does not contain "+KeyValueTokens.VALUE_TOKEN+" token."); } }
🌐
Javadoc.io
javadoc.io › doc › com.fasterxml.jackson.core › jackson-databind › 2.9.5 › com › fasterxml › jackson › databind › class-use › JsonNode.html
Uses of Class com.fasterxml.jackson.databind.JsonNode
Bookmarks · Latest version of com.fasterxml.jackson.core:jackson-databind · https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-databind · Current version 2.9.5 · https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-databind/2.9.5 · package-list path (used for javadoc generation ...
🌐
Webdevtutor
webdevtutor.net › blog › c-sharp-jsonnode-get-value-by-path
How to Access JSON Node Value by Path in C#
Assuming you have a JSON string or object, you can parse it using Json.NET and then access the desired value by providing the path.
🌐
Baeldung
baeldung.com › home › json › jackson › using findvalue() to get the value for a nested key in jackson
Using findValue() to Get the Value for a Nested Key in Jackson | Baeldung
September 5, 2024 - String jsonString = "{ \"organization\": { \"department\": { \"team\": { \"lead\": { \"name\": \"Alice\", \"contact\": { \"email\": \"[email protected]\" } } } } } }"; ObjectMapper mapper = new ObjectMapper(); JsonNode rootNode = mapper.readTree(jsonString); We then use the at() method to navigate through the nested structure: String email = rootNode.at("/organization/department/team/lead/contact/email").asText(); assertEquals("[email protected]", email); Here, the path provided to the at() method is a JSON Pointer, which is a standardized way to navigate through a JSON document using string syntax.
🌐
Fasterxml
fasterxml.github.io › jackson-core › javadoc › 1.9 › org › codehaus › jackson › JsonNode.html
JsonNode (Jackson JSON Processor)
@Deprecated public final JsonNode getPath(int index) Deprecated. Use path(int) instead · Alias of path(int). ... Method that can be called on object nodes, to access a property that has object value; or if no such property exists, to create and return such object node.