Suppose my JSON is :
{
"Name" : "Anmol Jain",
"Address" : [
{
"type" : "home",
"category" : "primary",
"street" : "ABC"
},
{
"type" : "home",
"category" : "secondary",
"street" : "XYZ"
},
{
"type" : "work",
"category" : "primary",
"street" : "PQR"
}
]
}
I am designing a specific syntax for my project so that I can query the JSON. For Example :
Address(type = home; category = secondary).street
this should give me result as "XYZ".
The first approach that comes to my mind is to parse the above code syntax, and look for conditions like type and category (by string parsing). then loop through Address array and try to match those conditions. But this will give higher time complexity in case of large JSONs.
Is there any library which does that. Or if anyone can suggest me better approach. The syntax for the condition is flexible and I can mould it accordingly.
Thanks in advance.
CodePudding user response:
Besides @tgdavies mentioned, also have a look at https://github.com/eiiches/jackson-jq, it uses the same syntax of jq, but in java implementation.
CodePudding user response:
For example, use library "Josson & Jossons"
https://github.com/octomix/josson
implementation 'com.octomix.josson:josson:1.3.22'
------------------------------------------------
Josson josson = Josson.fromJsonString(
"{"
" \"Name\" : \"Anmol Jain\","
" \"Address\" : ["
" {"
" \"type\" : \"home\","
" \"category\" : \"primary\","
" \"street\" : \"ABC\""
" },"
" {"
" \"type\" : \"home\","
" \"category\" : \"secondary\","
" \"street\" : \"XYZ\""
" },"
" {"
" \"type\" : \"work\","
" \"category\" : \"primary\","
" \"street\" : \"PQR\""
" }"
" ]"
"}");
String street = josson.getString("Address[type='home' & category='secondary'].street");