To do that you can use Jayway JsonPath library. It allows to find paths for given property. When you find all paths you can extract values for them. JSON Schema
which describes JSON
is also a valid JSON
so firstly, you can extract all properties from JSON Schema
and after that proces given JSON
payload. In below example I use predefined list of properties.
For given JSON
payload (assume that all *photo
properties are described in schema as images):
{
"map": {
"photo": "map photo"
},
"person": {
"data": {
"photos": {
"photo": "photo Base64",
"passportPhoto": "passport photo Base64"
}
}
}
}
Below example:
import com.jayway.jsonpath.EvaluationListener;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class JsonPathApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
List<String> paths = new ArrayList<>();
ReadContext findPathsContext = JsonPath.parse(jsonFile).withListeners((found) -> {
paths.add(found.path());
return EvaluationListener.EvaluationContinuation.CONTINUE;
});
List<String> properties = Arrays.asList("photo", "passportPhoto");
properties.forEach(p -> findPathsContext.read("$.." + p));
ReadContext readContext = JsonPath.parse(jsonFile);
for (String path : paths) {
System.out.println("Path: " + path);
System.out.println("Value: " + readContext.read(path));
}
}
}
Prints:
Path: $['map']['photo']
Value: map photo
Path: $['person']['data']['photos']['photo']
Value: photo Base64
Path: $['person']['data']['photos']['passportPhoto']
Value: passport photo Base64
See also:
- Jsonpath with Jackson or Gson
- Json Path Maven