|
| 1 | +package com.baeldung.jackson.jsonnode; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Iterator; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Map.Entry; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.core.JsonFactory; |
| 11 | +import com.fasterxml.jackson.core.JsonParser; |
| 12 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 13 | +import com.fasterxml.jackson.core.JsonToken; |
| 14 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 15 | +import com.fasterxml.jackson.databind.JsonNode; |
| 16 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 17 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 18 | + |
| 19 | +public class GetAllKeysFromJSON { |
| 20 | + |
| 21 | + public static List<String> getKeysInJsonUsingMaps(String json, ObjectMapper mapper) { |
| 22 | + List<String> keys = new ArrayList<>(); |
| 23 | + |
| 24 | + try { |
| 25 | + Map<String, Object> jsonElements = mapper.readValue(json, new TypeReference<Map<String, Object>>() { |
| 26 | + }); |
| 27 | + getAllKeys(jsonElements, keys); |
| 28 | + return keys; |
| 29 | + |
| 30 | + } catch (JsonProcessingException e) { |
| 31 | + e.printStackTrace(); |
| 32 | + } |
| 33 | + |
| 34 | + return keys; |
| 35 | + } |
| 36 | + |
| 37 | + public static void getAllKeys(Map<String, Object> jsonElements, List<String> keys) { |
| 38 | + |
| 39 | + jsonElements.entrySet() |
| 40 | + .forEach(entry -> { |
| 41 | + keys.add(entry.getKey()); |
| 42 | + if (entry.getValue() instanceof Map) { |
| 43 | + Map<String, Object> map = (Map<String, Object>) entry.getValue(); |
| 44 | + getAllKeys(map, keys); |
| 45 | + } else if (entry.getValue() instanceof List) { |
| 46 | + List<?> list = (List<?>) entry.getValue(); |
| 47 | + list.forEach(listEntry -> { |
| 48 | + if (listEntry instanceof Map) { |
| 49 | + Map<String, Object> map = (Map<String, Object>) listEntry; |
| 50 | + getAllKeys(map, keys); |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + public static List<String> getKeysInJsonUsingJsonNodeFieldNames(String json, ObjectMapper mapper) { |
| 58 | + List<String> keys = new ArrayList<>(); |
| 59 | + |
| 60 | + try { |
| 61 | + JsonNode jsonNode = mapper.readTree(json); |
| 62 | + Iterator<String> iterator = jsonNode.fieldNames(); |
| 63 | + iterator.forEachRemaining(e -> keys.add(e)); |
| 64 | + |
| 65 | + } catch (JsonProcessingException e) { |
| 66 | + e.printStackTrace(); |
| 67 | + } |
| 68 | + return keys; |
| 69 | + } |
| 70 | + |
| 71 | + public static List<String> getAllKeysInJsonUsingJsonNodeFieldNames(String json, ObjectMapper mapper) { |
| 72 | + List<String> keys = new ArrayList<>(); |
| 73 | + |
| 74 | + try { |
| 75 | + JsonNode jsonNode = mapper.readTree(json); |
| 76 | + getAllKeysUsingJsonNodeFieldNames(jsonNode, keys); |
| 77 | + |
| 78 | + } catch (JsonProcessingException e) { |
| 79 | + e.printStackTrace(); |
| 80 | + } |
| 81 | + return keys; |
| 82 | + } |
| 83 | + |
| 84 | + public static List<String> getAllKeysInJsonUsingJsonNodeFields(String json, ObjectMapper mapper) { |
| 85 | + List<String> keys = new ArrayList<>(); |
| 86 | + |
| 87 | + try { |
| 88 | + JsonNode jsonNode = mapper.readTree(json); |
| 89 | + getAllKeysUsingJsonNodeFields(jsonNode, keys); |
| 90 | + |
| 91 | + } catch (JsonProcessingException e) { |
| 92 | + e.printStackTrace(); |
| 93 | + } |
| 94 | + return keys; |
| 95 | + } |
| 96 | + |
| 97 | + public static void getAllKeysUsingJsonNodeFields(JsonNode jsonNode, List<String> keys) { |
| 98 | + |
| 99 | + if (jsonNode.isObject()) { |
| 100 | + Iterator<Entry<String, JsonNode>> fields = jsonNode.fields(); |
| 101 | + |
| 102 | + fields.forEachRemaining(field -> { |
| 103 | + keys.add(field.getKey()); |
| 104 | + getAllKeysUsingJsonNodeFieldNames((JsonNode) field.getValue(), keys); |
| 105 | + }); |
| 106 | + } else if (jsonNode.isArray()) { |
| 107 | + ArrayNode arrayField = (ArrayNode) jsonNode; |
| 108 | + arrayField.forEach(node -> { |
| 109 | + getAllKeysUsingJsonNodeFieldNames(node, keys); |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + public static void getAllKeysUsingJsonNodeFieldNames(JsonNode jsonNode, List<String> keys) { |
| 116 | + |
| 117 | + if (jsonNode.isObject()) { |
| 118 | + Iterator<String> fieldNames = jsonNode.fieldNames(); |
| 119 | + |
| 120 | + fieldNames.forEachRemaining(fieldName -> { |
| 121 | + keys.add(fieldName); |
| 122 | + getAllKeysUsingJsonNodeFieldNames(jsonNode.get(fieldName), keys); |
| 123 | + }); |
| 124 | + } else if (jsonNode.isArray()) { |
| 125 | + ArrayNode arrayField = (ArrayNode) jsonNode; |
| 126 | + arrayField.forEach(node -> { |
| 127 | + getAllKeysUsingJsonNodeFieldNames(node, keys); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + public static List<String> getKeysInJsonUsingJsonParser(String json, ObjectMapper mapper) { |
| 134 | + List<String> keys = new ArrayList<>(); |
| 135 | + |
| 136 | + try { |
| 137 | + JsonNode jsonNode = mapper.readTree(json); |
| 138 | + JsonParser jsonParser = jsonNode.traverse(); |
| 139 | + while (!jsonParser.isClosed()) { |
| 140 | + if (jsonParser.nextToken() == JsonToken.FIELD_NAME) { |
| 141 | + keys.add((jsonParser.getCurrentName())); |
| 142 | + } |
| 143 | + } |
| 144 | + } catch (JsonProcessingException e) { |
| 145 | + e.printStackTrace(); |
| 146 | + } catch (IOException e) { |
| 147 | + e.printStackTrace(); |
| 148 | + } |
| 149 | + |
| 150 | + return keys; |
| 151 | + } |
| 152 | + |
| 153 | + public static List<String> getKeysInJsonUsingJsonParser(String json) { |
| 154 | + List<String> keys = new ArrayList<>(); |
| 155 | + |
| 156 | + try { |
| 157 | + JsonFactory factory = new JsonFactory(); |
| 158 | + JsonParser jsonParser = factory.createParser(json); |
| 159 | + while (!jsonParser.isClosed()) { |
| 160 | + if (jsonParser.nextToken() == JsonToken.FIELD_NAME) { |
| 161 | + keys.add((jsonParser.getCurrentName())); |
| 162 | + } |
| 163 | + } |
| 164 | + } catch (JsonProcessingException e) { |
| 165 | + e.printStackTrace(); |
| 166 | + } catch (IOException e) { |
| 167 | + e.printStackTrace(); |
| 168 | + } |
| 169 | + |
| 170 | + return keys; |
| 171 | + } |
| 172 | +} |
0 commit comments