How can I Assert the number of child elements in JSON

UK_UK_ Posts: 11

I have 20 child elements for the parent element in my JSON. How can I assert the number of child elements is returned the same? I only want to assert the number of child elements.

Comments

  • jakubiakjakubiak Posts: 384 admin

    Use an Occurrence assertion (under Structure assertions).

  • UK_UK_ Posts: 11

    Using Occurrence assertion I can only asset the elements returned.

    Here I just want the assertion that there are 20 elements returned in my parent element. I do not need individual elements asserted.

  • UK_UK_ Posts: 11

    Do we know how we can assert only the number of elements returned back

  • jakubiakjakubiak Posts: 384 admin
    edited November 1

    Let's say I have the following JSON:

    {
        "foo" : {
            "a" : 1,
            "b" : 2
        }
    }
    

    I want to verify that the element "foo" has two elements, but I don't care what they are. Then I create an Occurrence Assertion with the XPath /root/foo/* that verifies that there are 2 elements. The wildcard * is how I can select multiple child elements regardless of what they are.

  • UK_UK_ Posts: 11

    It works, thanks!

Sign In or Register to comment.