Forum Discussion

richie's avatar
richie
Community Hero
5 years ago
Solved

Check repeating .json attribute has either of two specific regex patterns

Hi!

 

A while back I needed some help to assert that a certain repeating attribute in a multi record json response had a specific regex pattern of 3 upper case chars.

 

Rao sorted me out - see below:

 

//code courtesy of Rao
assert context.response, 'Request parameter is correct'
def json = new groovy.json.JsonSlurper().parseText(context.response)
assert json.data.Name.every{it ==~ /[A-Z]{3}/}

I've now come across a situation where I need to specify the regex pattern for either of two pattern types - e.g. the repeating json attribute in my multi record json response will display an attribute value of 'AD' or 'GB-NIF'

 

I tried adding a vertical bar in to separate two regexes but that didn't work (e.g.

//Rich's bodge of Rao's good work
assert context.response, 'Request parameter is correct'
def json = new groovy.json.JsonSlurper().parseText(context.response)
assert json.value.code.every{it ==~ /[A-Z]{2}/ | /[A-Z]{2}-[A-Z]{3}/} //where /[A-Z]{2}/ == 'AD' and /[A-Z]{2}-[A-Z]{3}} == 'GB-NIF'

I added in the red text above to try and add in an OR operator for the regex so it could match either 'GB' or 'GB-NIF' - but it didn't work.

 

I've had a bit of a google for the last hour and found that the / indicates the end of the regex, so I tried the changing the above to the following:

//Rich's 2nd bodge of Rao's good work
assert context.response, 'Request parameter is correct'
def json = new groovy.json.JsonSlurper().parseText(context.response)
assert json.value.code.every{it ==~ /[A-Z]{2} | [A-Z]{2}-[A-Z]{3}/} 

But the above doesn't work either - perhaps because it's not recognising the | as outside the regex pattern now I've removed the initial ending / (after the first regex value)

 

I tried a couple of other options - but still no luck

 

Would anyone know how to assert that a specific repeating attribute can be either of two regex pattern values? (e.g. either 'GB' or 'XX-NOF')??

 

An example .json response file I've been working with is as follows:

{
   "@odata.context": "https://whatevs.azurewebsites.net/CaseManagement.svc/$metadata#CommodityGroups",
   "value":    [
      {
         "id": "fcc9bc3c-ebe3-49ca-8a10-725cbbebba3b",
         "name": "EQUINE",
         "code": "GB"
      },
      {
         "id": "6606760c-f525-450a-8067-bb820ac5ebe4",
         "name": "FISH & FISH PRODUCTS",
         "code": "AD"
      },
      {
         "id": "f285cb31-7918-454c-9304-54f1e5cbfd3a",
         "name": "GERMPLASM",
         "code": "GB-NIF"
      },
...
      {
         "id": "92595e56-e7d3-4d34-8009-cb4d2d863085",
         "name": "POULTRY",
         "code": "XX-AIK"
      }
   ]
}

I'd welcome any guidance in regards to the above,

 

Thanks,

 

Richie

 

 

  • richie 

    Oh, data with valid and invalid could have helped to write the assertions.

     

    Anyways, seems you have trivial issue in the code snippet

     

    The following should work if your regex are correct

    assert json.value.code.every{it ==~ /[A-Z]{2}/ || it ==~ /[A-Z]{2}-[A-Z]{3}/}
  • richie 

    The regex further can be simplified as (not tested though):

    ([A-Z]{2}(-[A-Z]{3})?)

     

    assert json.value.code.every{it ==~ /([A-Z]{2}(-[A-Z]{3})?)/}

     

     

5 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    richie 

    Not sure if regex is need in this case as you have specified the values. Please try below

     

    assert json.value.code.every{it in ['GB', 'GB-NIF']} 
    • richie's avatar
      richie
      Community Hero
      Hey nmrao,

      My bad for not being clear.....those values are iso country and region codes....hence the reason why i think i need to use regex...

      Ta,

      Richie
      • nmrao's avatar
        nmrao
        Champion Level 3

        richie 

        Oh, data with valid and invalid could have helped to write the assertions.

         

        Anyways, seems you have trivial issue in the code snippet

         

        The following should work if your regex are correct

        assert json.value.code.every{it ==~ /[A-Z]{2}/ || it ==~ /[A-Z]{2}-[A-Z]{3}/}