My advice is to load the json into Map<String, Object> and then to recursively iterate on the map and collect the data you require
Here is the entire solution for you
public class JsonProperties
{
@SuppressWarnings("unchecked")
public static void main(String[] args)
{
ObjectMapper mapper = new ObjectMapper();
try (InputStream is = new FileInputStream("C://Temp/xx.json")) {
Map<String, Object> map = mapper.readValue(is, Map.class);
List<Property> properties = getProperties((Map<String, Object>)map.get("properties"));
System.out.println(properties);
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static List<Property> getProperties(Map<String, Object> propertiesMap) {
List<Property> propertiesList = new ArrayList<>();
// iterate on properties
for (Map.Entry<String, Object> propertyEntry : propertiesMap.entrySet()) {
Property property = new Property();
property.name = propertyEntry.getKey();
Map<String, Object> propertyAttrsMap = (Map<String, Object>)propertyEntry.getValue();
property.type = (String)propertyAttrsMap.get("type");
// recursively get sub properties
if (propertyAttrsMap.containsKey("properties")) {
property.subProperties = getProperties((Map<String, Object>)propertyAttrsMap.get("properties"));
}
propertiesList.add(property);
}
return propertiesList;
}
public static class Property {
public String name;
public String type;
public List<Property> subProperties = new ArrayList<>();
@Override
public String toString() {
return name + " " + type + " " + subProperties;
}
}
}
using this sample input
{
"description": "Something",
"id": "abc.def.xyzjson#",
"type": "object",
"properties": {
"triggerTime": {
"type": "string",
"description": "Time of adjustment event",
"source": "ab.cd",
"pattern": "something",
"required": true
},
"customerId": {
"type": "string",
"description": "Something",
"source": "ef.gh",
"required": true
},
"complex": {
"type": "string",
"description": "Something",
"source": "ef.gh",
"required": true,
"properties": {
"sub1": {
"type": "int",
"description": "sub1",
"source": "ab.cd",
"required": true
},
"sub2": {
"type": "short",
"description": "sub2",
"source": "ab.cd",
"required": true
}
}
}
}
}
produces the following output:
[triggerTime string [], customerId string [], complex string [sub1 int [], sub2 short []]]