Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

How can I Assert the number of child elements in JSON

UK_
UK_ Posts: 22

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

  • jakubiak
    jakubiak Posts: 795 admin

    Use an Occurrence assertion (under Structure assertions).

  • UK_
    UK_ Posts: 22

    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: 22

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

  • jakubiak
    jakubiak Posts: 795 admin
    edited November 2018

    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: 22

    It works, thanks!