cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Check array is empty or not

Gayathry
New Contributor II

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.

7 REPLIES 7

bojanvelevski
Valued Contributor

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

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โ€: [
{}
]

@Gayathry, go with the following expression:

$group.filter(x=>x.hasPath('ID')).length > 0

@bojanvelevski: Thank you for sharing this.
Can we check if the incoming JSON is empty or not using the same?

Thanks.