Forum Discussion

Gayathry's avatar
Gayathry
New Contributor II
4 years ago

Check array is empty or not

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

  • Gayathry's avatar
    Gayathry
    New Contributor II

    Thank you so much. It worked.

  • bojanvelevski's avatar
    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

    • Gayathry's avatar
      Gayathry
      New Contributor II

      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's avatar
    Gayathry
    New Contributor II

    Thanks a lot Bojan. instead if using ‘ID’ in json path expression is there any other way to check list is empty as the object ‘ID’ name is a dynamic one.