How to find value with dynamic node name using Jackson and JsonPointer
<p>I am using <a href="https://github.com/FasterXML/jackson" rel="nofollow noreferrer">Jackson</a> (version 2.6+) to parse some <em>ugly</em> JSON that looks like this:</p>
<pre><code>{ 
    &quot;root&quot; : { 
        &quot;dynamic123&quot; : &quot;Some value&quot;
    }
}
</code></pre>
<p>The name of the attribute <code>dynamic123</code> is unfortunately not known until runtime and may differ from time to time. What I am trying to achieve is to use <a href="https://github.com/FasterXML/jackson-core/blob/master/src/main/java/com/fasterxml/jackson/core/JsonPointer.java" rel="nofollow noreferrer">JsonPointer</a> to get to the value <code>&quot;Some value&quot;</code>. <a href="https://github.com/FasterXML/jackson-core/blob/master/src/main/java/com/fasterxml/jackson/core/JsonPointer.java" rel="nofollow noreferrer">JsonPointer</a> uses an <em>XPath</em>-like syntax that is <a href="https://datatracker.ietf.org/doc/html/draft-ietf-appsawg-json-pointer-03" rel="nofollow noreferrer">described here</a>.</p>
<pre><code>// { &quot;root&quot; : { &quot;dynamic123&quot; : &quot;Some value&quot; } }
ObjectNode json = mapper.createObjectNode();
json.set(&quot;root&quot;, json.objectNode().put(&quot;dynamic123&quot;, &quot;Some value&quot;));

// Some basics
JsonNode document = json.at(&quot;&quot;);                      // Ok, the entire document
JsonNode missing = json.at(&quot;/missing&quot;);               // MissingNode (as expected)
JsonNode root = json.at(&quot;/root&quot;);                     // Ok -&gt; { dynamic123 : &quot;Some value&quot; }

// Now, how do I get a hold of the value under &quot;dynamic123&quot; when I don't
// know the name of the node (since it is dynamic)
JsonNode obvious = json.at(&quot;/root/dynamic123&quot;);       // Duh, works. But the attribute name is unfortunately unknown so I can't use this
JsonNode rootWithSlash = json.at(&quot;/root/&quot;);           // MissingNode, does not work
JsonNode zeroIndex = json.at(&quot;/root[0]&quot;);             // MissingNode, not an array
JsonNode zeroIndexAfterSlash = json.at(&quot;/root/[0]&quot;);  // MissingNode, does not work
</code></pre>
<p>So, now to my question. Is there a way of retrieving the value <code>&quot;Some value&quot;</code> using <a href="https://github.com/FasterXML/jackson-core/blob/master/src/main/java/com/fasterxml/jackson/core/JsonPointer.java" rel="nofollow noreferrer">JsonPointer</a>?</p>
<p>Obviously there are other ways of retrieving the value. One possible approach is to use the <code>JsonNode</code> traversal functions – e.g. like this:</p>
<pre><code>JsonNode root = json.at(&quot;/root&quot;);
JsonNode value = Optional.of(root)
        .filter(d -&gt; d.fieldNames().hasNext()) // verify that there are any entries
        .map(d -&gt; d.fieldNames().next())       // get hold of the dynamic name
        .map(name -&gt; root.get(name))           // lookup of the value
        .orElse(MissingNode.getInstance());    // if it is missing
</code></pre>
<p>But, I am trying to avoid traversal and only use <a href="https://github.com/FasterXML/jackson-core/blob/master/src/main/java/com/fasterxml/jackson/core/JsonPointer.java" rel="nofollow noreferrer">JsonPointer</a>.</p>