Question:
I have a service that takes in a JSON
string as input. The JSON
schema is different every time where some fields are not always present. How can I query the values of those fields using Jayway's JsonPath
when they are present?
What I've tried:
I used the Option.DEFAULT_PATH_LEAF_TO_NULL
as Jayway's readme page explained
Configuration config = Configuration.defaultConfiguration()
.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
if (JsonPath.isPathDefinite(attribute.jsonPath))
{
String value = JsonPath.using(config).parse(currentTx).read(attribute.jsonPath);
if (value != null)
{
attributeValues.add(value);
}
}
else
{
List<String> attributeValuesArray = JsonPath.using(config).parse(currentTx).read(attribute.jsonPath);
for (String value : attributeValuesArray)
{
if (value != null)
{
attributeValues.add(value);
}
}
}
This should make JsonPath.read()
return null
if the path is not found, however my code still throws:
com.jayway.jsonpath.PathNotFoundException: Missing property in path $['somepath']
when I give it a inexistent path. Does anyone know what might be causing this?