Forum Discussion

richie's avatar
richie
Community Hero
6 years ago

JSONPATH - MULTIPLE RECORD SYNTAX IN GROOVYSCRIPT?

Hi,

 

I always struggle with JSONPATH relative to XPATH and this is another instance.

 

I have an assertion in my groovy script with the following syntax for a single record in the resultset - just to clarify - the total attribute within the data node has a value of 'Rich' (JSONPATH generated by readyAPI! would generate $[data][Name])

 

assert object.data.Name == 'Rich'

BUT - how do I represent multiplicity for multiple records in my resultset?

 

i.e. if I had 2 records in my result set - so there are 2 'Name' attributes in the data node - the JSONPATH to the individual Name attributes is as follows:

$['data'][0]['Name']
$['data'][1]['Name']

The JSONPATH notation in my groovy differs - does anyone know how to create a specific assertion on each of the above 'Name' attributes?

 

I tried 

assert object.data.0.Name == 'Rich'

and 

assert object.data.[0].Name == 'Rich'

 

As always?  I appreciate all/any guidance/help/education anyone's willing to impart! :)

 

Cheers,

 

richie

 

 

  • Are you expecting result is list of names?

    If so,

    assert object.data.Name == ['name1', 'name2'...]

    Does it help?
  • nmrao's avatar
    nmrao
    Champion Level 3
    Are you expecting result is list of names?

    If so,

    assert object.data.Name == ['name1', 'name2'...]

    Does it help?
    • richie's avatar
      richie
      Community Hero

      aaah!

       

      So you're saying rather than have individual assertions that definitely describe their location in the response - just have one assertion with the expected results being an array of values?

       

      hahahaha! that'll do!

       

      Thanks man!

       

      richie!

       

       

      • nmrao's avatar
        nmrao
        Champion Level 3
        Exactly. It is completely ok to have single assertion which can handle either simple or complex checks.