โ07-11-2021 02:45 AM
Hi Experts,
I have a requirement where I need to check โgroupโ array is empty or not. Could someone help with the JSON path expression.
Empty group array JSON looks like:
[
{
โgroupโ: [
{}
]
}
]
Non empty group array json loiks like:
[
{
โgroupโ: [
{
โIDโ: โ002โ
}
{
โIDโ: โ003โ
}
]
}
]
Thanks in Advance.
โ07-11-2021 07:32 AM
Hey @Gayathry,
Check itโs length with the .length method:
$.group.length > 0
Case 1 will return false, Case 2 will return true.
Regards,
Bojan
โ07-11-2021 08:11 AM
Thanks Bojan for your quick reply.
Actually as per my requirement inside โgroupโ if getting a empty list โ{}โ, it should return false . But group.lengh is returning true even if list is empty(ie {}).
eg:
if JSON is like below it should return false.
โgroupโ: [
{}
]
โ07-11-2021 12:37 PM
@Gayathry, go with the following expression:
$group.filter(x=>x.hasPath('ID')).length > 0
โ02-21-2023 06:34 AM
@bojanvelevski: Thank you for sharing this.
Can we check if the incoming JSON is empty or not using the same?
Thanks.