You can use elements() method and check if value key exist then add the value to list.

Smaple code

ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(data);

List<String> values = new ArrayList<>();
jsonNode.forEach(jsonObject -> jsonObject.elements().forEachRemaining(valueNode -> {
    if(valueNode.has("value"))
        values.add(valueNode.get("value").asText());
}));
System.out.println(values);

Output:

[http://www.wikidata.org/entity/Q42442324, http://www.wikidata.org/prop/direct/P21, Kiisu Miisu, http://www.wikidata.org/entity/Q43260736, http://www.wikidata.org/prop/direct/P21, Paddles]
Answer from deadshot on Stack Overflow
🌐
Baeldung
baeldung.com › home › json › jackson › get all the keys in a json string using jsonnode
Get all the Keys in a JSON String Using JsonNode | Baeldung
May 11, 2024 - Now let’s discuss different approaches to fetch keys from a JSON. We can use the fieldNames() method on a JsonNode instance to fetch the nested field names. It returns names of direct nested fields only.
Top answer
1 of 2
2

You can use elements() method and check if value key exist then add the value to list.

Smaple code

ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(data);

List<String> values = new ArrayList<>();
jsonNode.forEach(jsonObject -> jsonObject.elements().forEachRemaining(valueNode -> {
    if(valueNode.has("value"))
        values.add(valueNode.get("value").asText());
}));
System.out.println(values);

Output:

[http://www.wikidata.org/entity/Q42442324, http://www.wikidata.org/prop/direct/P21, Kiisu Miisu, http://www.wikidata.org/entity/Q43260736, http://www.wikidata.org/prop/direct/P21, Paddles]
2 of 2
0

Here is the solution by "Josson & Jossons". I list 2 more examples with condition filtering.

https://github.com/octomix/josson

implementation 'com.octomix.josson:josson:1.3.22'

---------------------------------------------

Josson josson = Josson.fromJsonString(
    "[" +
    "  {" +
    "    \"item\": {" +
    "      \"type\": \"uri\", \"value\": \"http://www.wikidata.org/entity/Q42442324\"" +
    "    }," +
    "    \"prop\": {" +
    "      \"type\": \"uri\", \"value\": \"http://www.wikidata.org/prop/direct/P21\"" +
    "    }," +
    "    \"itemLabel\": {" +
    "      \"xml:lang\": \"en\", \"type\": \"literal\", \"value\": \"Kiisu Miisu\"" +
    "    }" +
    "  }," +
    "  {" +
    "    \"item\": {" +
    "      \"type\": \"uri\", \"value\": \"http://www.wikidata.org/entity/Q43260736\"" +
    "    }," +
    "    \"prop\": {" +
    "      \"type\": \"uri\", \"value\": \"http://www.wikidata.org/prop/direct/P21\"" +
    "    }," +
    "    \"itemLabel\": {" +
    "      \"xml:lang\": \"en\", \"type\": \"literal\", \"value\": \"Paddles\"" +
    "    }" +
    "  }" +
    "]");

JsonNode node = josson.getNode("*.value");
System.out.println("1.\n" + node.toPrettyString());

node = josson.getNode("~'^item.*'.value");
System.out.println("2.\n" + node.toPrettyString());

node = josson.getNode("*[value.type='uri']*.value");
System.out.println("3.\n" + node.toPrettyString());

Output:

1.
[ "http://www.wikidata.org/entity/Q42442324", "http://www.wikidata.org/prop/direct/P21", "Kiisu Miisu", "http://www.wikidata.org/entity/Q43260736", "http://www.wikidata.org/prop/direct/P21", "Paddles" ]
2.
[ "http://www.wikidata.org/entity/Q42442324", "Kiisu Miisu", "http://www.wikidata.org/entity/Q43260736", "Paddles" ]
3.
[ "http://www.wikidata.org/entity/Q42442324", "http://www.wikidata.org/prop/direct/P21", "http://www.wikidata.org/entity/Q43260736", "http://www.wikidata.org/prop/direct/P21" ]
Discussions

JSON Node to a Key/Value
Hi, I have a JSON table coming in like this: { “344-2-3-pt_2_fsfsf1_GDT” : { “id” : “fsfsf1”, “type” : “single”, “groupid” : “2”, “islast” : “0”, “max_score” : “”, “context” : “GDT” }, “344-2-3-pt_2_fsfsf2_GDT… More on forum.knime.com
🌐 forum.knime.com
1
1
August 26, 2021
How to iterate through all keys of json node
Cannot convert type 'KeyValuePair(Of String, JsonNode)' in JsonNode · I want to parse and get all the values of the property "last". Is there a way to make that code work in my case? I'm trying to avoid to write all the properties because they are more than 1000 values ... An object-oriented programming language developed by ... More on learn.microsoft.com
🌐 learn.microsoft.com
1
0
Get all the keys in a JSON string JsonNode in java - Stack Overflow
I have a json string which I need to validate and find any other keys other than in a list is there in the json string. The sample json string is { "required" : true, "requir... More on stackoverflow.com
🌐 stackoverflow.com
java - check if a jsonNode contains a specific key then EXTRACT its value once it exists - Stack Overflow
I have this String an I covered it to jsonNode using the ObjectMapper. Then I tried to look for a pecific key in this jsonNode so I used the ".has" but it did not work !! Here is what I d... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Jenkov
jenkov.com › tutorials › java-json › jackson-jsonnode.html
Jackson JsonNode
The JsonNode class has a method named fieldNames() which returns an Iterator that enables you to iterate all the field names of the JsonNode. You can use the field names to get the field values.
🌐
Adobe Experience League
experienceleaguecommunities.adobe.com › t5 › adobe-experience-manager › extract-keys-from-json-node › m-p › 209101
Solved: Extract keys from Json Node - Adobe Experience League Community - 209101
February 16, 2016 - I do have a com.fasterxml.jackson.databind.JsonNode · format like this : { "jcr:primaryType": "nt:unstructured", "sling:resourceType": "foundation/components/parsys", "rwdcarousel": { "jcr:primaryType": "nt:unstructured", "jcr:createdBy": "admin", "contentFinderImage": "/etc/designs/webcms-ngtv/globallibs/img/rwdCarouselModuleIcon.jpg", "rwdcarouselType": "singleCarousel", "jcr:lastModifiedBy": "admin" } } Out of this, I need to fetch keys which contains values inside { }. Like, in this case, rwdcarousel.
🌐
Tabnine
tabnine.com › home page › code › java › org.codehaus.jackson.jsonnode
org.codehaus.jackson.JsonNode.getFields java code examples | Tabnine
private Map<String, Set<String>> ... = node.getFields(); while ( nodeIterator.hasNext() ) { Entry<String, JsonNode> oneEntry = nodeIterator.next(); String key = oneEntry.getKey(); Set<String> valueSet = new HashSet<String>(); JsonNode valueNodes ...
🌐
Sourceforge
weka.sourceforge.io › doc.dev › weka › core › json › JSONNode.html
JSONNode
Returns whether the node stores a primitive value or a an array/object. ... Returns wether the node is an array. ... Returns wether the node is an object. ... Returns the type of the container. ... Adds a "null" child to the object. ... Adds a key-value child to the object.
Find elsewhere
🌐
Baeldung
baeldung.com › home › json › jackson › working with tree model nodes in jackson
Working with Tree Model Nodes in Jackson | Baeldung
January 8, 2024 - Instead, we’ll use JsonNode.fields because this gives us access to both the field name and value: Key="name", Value=Object {"first": "Tatu", "last": "Saloranta"} Key="title", Value=Value "Jackson Founder" Key="company", Value=Value "FasterXML" Key="pets", Value=Array [{"type": "dog", "number": 1},{"type": "fish", "number": 50}] For each field, we add the field name to the output and then process the value as a child node by passing it to the processNode method: private void appendNodeToYaml( JsonNode node, StringBuilder yaml, int depth, boolean isArrayItem) { Iterator<Entry<String, JsonNode>
Top answer
1 of 2
12

If you're trying to find a key which is placed inside nested object, you may use findValue(String key) method which returns null if a value is not found by the given key:

ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode= mapper.readTree(json);

String[] keys = {
    "id", "create_date", "versions_control_advanced", "name", "nofield"
};

for (String key : keys) {
    JsonNode value = rootNode.findValue(key);
    System.out.printf("Key %s exists? %s --> value=%s%n", key, value != null,
                    value == null ? null : value.asText());

}

Output:

Key id exists? true --> value=276625
Key create_date exists? true --> value=2020-06-22T16:19:07
Key versions_control_advanced exists? true --> value=false
Key name exists? true --> value=
Key nofield exists? false --> value=null
2 of 2
0

I think you are not bound to the has() method.

You can convert the json to a map and then find the node recursively

    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> map = mapper.readValue( body, Map.class );

    ArrayList<Object> container = new ArrayList<>();
    boolean value = find( map, "id", container );

    if( value )
    {
        System.out.println( container );
    }

The recursive method should visit all the nodes and return soon as node is found

private static boolean find( Map<String, Object> map, String search, ArrayList<Object> container )
    {
        int i = 0;
        for( String s : map.keySet() )
        {
            i++;
            if( s.equals( search ) )
            {
                container.add( map.get( s ) );
                return true;
            }
            if( map.get( s ) instanceof Map )
            {
                boolean found = find( (Map<String, Object>) map.get( s ), search, container );
                if( i == map.size() || found )
                {
                    return found;
                }
            }
        }
        return false;
    }

I have edited the code to get the value also. hope this helps. I strongly suggest you to do more research on yourself before looking for help from the community.

🌐
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.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.text.json.nodes.jsonobject.system-collections-generic-idictionary-system-string-system-text-json-nodes-jsonnode--keys
JsonObject.IDictionary<String,JsonNode>.Keys Property (System.Text.Json.Nodes) | Microsoft Learn
Gets a collection containing the property names in the JsonObject. property System::Collections::Generic::ICollection<System::String ^> ^ System::Collections::Generic::IDictionary<System::String,System::Text::Json::Nodes::JsonNode>::Keys { ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-access-the-json-fields-arrays-and-nested-objects-of-jsonnode-in-java
How to access the JSON fields, arrays and nested objects of JsonNode in Java?
May 13, 2025 - To read JSON into a JsonNode with Jackson by creating an ObjectMapper instance and call the readValue() method. We can access a field, array or nested object using the get() method of the JsonNode class. We can return a valid string representation using the asText() method and convert the value ...
🌐
Fasterxml
fasterxml.github.io › jackson-databind › javadoc › 2.7 › com › fasterxml › jackson › databind › JsonNode.html
JsonNode (jackson-databind 2.7.0 API)
Iterator that can be used to traverse all key/value pairs for object nodes; empty iterator (no contents) for other types · public abstract JsonNode findValue(String fieldName)
🌐
Nim
nim-lang.org › docs › json.html
std/json
To retrieve the value of "key" you can do the following: import std/json let jsonNode = parseJson("""{"key": 3.14}""") doAssert jsonNode["key"].getFloat() == 3.14 · Important: The [] operator will raise an exception when the specified field does not exist. By using the {} operator instead ...
🌐
Stack Overflow
stackoverflow.com › questions › 66024879 › how-to-access-the-value-from-a-key-value-pair-in-a-jsonnode
java - How to access the value from a key-value pair in a jsonnode - Stack Overflow
public void getColorCode() throws JsonProcessingException { String color = "{\"Pink\":[\"#000000\"],\"Red\":[\"#000000\"],\"Blue\":[\"#000000\"],\"Orange\":[\"#000000\"]}"; ObjectMapper mapper = new ObjectMapper(); JsonNode node = mapper.readTree(color); for (JsonNode colorCode : node.get("Pink")){ System.out.println(colorCode); } } ... Find the answer to your question by asking.