I have an expected json file as below. It is a nested object
{
"contact_info": [{
"contact_id": "Contact_001",
"contact_status": {
"exam_status": 0,
"term_list": [{
"term_code": 110,
"t_list": [{
"s_code": "12001",
"sexam_status": 0
},
{
"s_code": "13001",
"sexam_status": 1
}
]
}]
}
}]
}
How to add all field name and value of all items inside this object into List? I tried using the following way but it is not as expected.
//Get all key name in json object
```public static List<String> getAllKeysInJsonUsingJsonNodeFieldNames(String json) {
ObjectMapper mapper = new ObjectMapper()
List<String> keys = new ArrayList<>();
JsonNode jsonNode = mapper.readTree(json);
getAllKeysUsingJsonNodeFields(jsonNode, keys);
return keys;
}
public static void getAllKeysUsingJsonNodeFields(JsonNode jsonNode, List<String> keys) {
if (jsonNode.isObject()) {
Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields();
fields.forEachRemaining({def field ->
keys.add(field.getKey());
getAllKeysUsingJsonNodeFields((JsonNode) field.getValue(), keys);
});
} else if (jsonNode.isArray()) {
ArrayNode arrayField = (ArrayNode) jsonNode;
arrayField.forEach({def node ->
getAllKeysUsingJsonNodeFields(node, keys);
});
}
}```
The result as below => It only shows only field name without figure out that field belong to which object
TagName[i] = s_code
I expect that the result should be below, specifically, I want to field name should be shown it belongs to which object => Could you tell me how to resolve it?
TagName[i] = contact_status.term_list[0].t_list[0].s_code
Sorry for I'm a new in Java => If anyone know a way to resolve it please tell me in more details. Thank you so much!