cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Highlighted
Community Hero

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

 

 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Community Hero

@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}/}


Regards,
Rao.

View solution in original post

Highlighted
Community Hero

@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})?)/}

 

 



Regards,
Rao.

View solution in original post

5 REPLIES 5
Highlighted
Community Hero

@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']} 


Regards,
Rao.
Highlighted
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
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Community Hero

@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}/}


Regards,
Rao.

View solution in original post

Highlighted
Community Hero

@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})?)/}

 

 



Regards,
Rao.

View solution in original post

Highlighted
Community Hero

I'm such an idiot sometimes - why couldn't I have gotten that?

 

@nmrao - saved my life again - Kudos awarded

 

nice one!

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Want a fun and easy way to learn ReadyAPI and show off your skills? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Get a full Raw JMS request

Participate!

Compare an expected JSON value and actual response in Events

Participate!

Fetch value/data from JSON response using Groovy Script

See replies!

Get data from Petstore and add it to Excel sheets

Participate!
Top Kudoed Authors