I have the following JSON:
{
"A":{
"C":{
"date":"2022-01-23"
}
},
"B":{
"D":{
"date":"2022-01-24"
}
}
}
I would like to extract the value $.A.C.date
if it is not null
, else I would like to extract $.B.D.date
.
Basically I would like to write an if else
expression using JsonPath
.
However, I don’t manage to make it work.
This is what I’ve tried:
String expression = "$.[?($.[?(@.A.C.date != null)].date || $.[?(@.B.D.date != null)].date)]";
JsonPath jsonPath = JsonPath.compile(expression);
String json = "...my JSON...";
Object extractedValue = jsonPath.read(json);
System.out.println(extractedValue);
… but the output I get is simply an empty array: []
.
I have also tried this:
"$.[?($.[?(@.A.C.date != null)].A.C.date || $.[?(@.B.D.date != null)].B.D.date)]"
… but the output is a list with all the elements (because conditions match in both cases), yet I thought that writing .A.C.date
was extracting just the date
and not everything:
[
{"A":{"C":{"date":"2022-01-23"}},
"B":{"D":{"date":"2022-01-24"}}}
]
Anyone has an idea? I can’t find anything online which is similar to this.
This is the dependency I’m using:
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
2
Answers
You may consider another library Josson to do the job.
https://github.com/octomix/josson
Output
Here is a possible solution using
com.jayway.jsonpath
It gets all the non-null dates then prints the first.