skip to Main Content

I have JSON for which I sometimes got some alphanumeric name in one key value name and sometimes it is null like below.

{"name":"77Watcher"}
{"name":null}

I have to validate the data I am using like below.

{"name":"${json-unit.matches:^([A-Za-z0-9]+|null)$}"}

But getting Matcher "^([A-Za-z0-9]+|null)$" not found.

If I use {"name":"${json-unit.matches:[A-z0-9]|null"} then getting <"${json-unit.matches:[A-z0-9]|null"> but was: <null>.

Is there any proper regex that I can put into my code for that?

2

Answers


  1. The way it’s written, isn’t it looking for "null" instead of null? So by putting the double quotes with the character grouping instead of around both the character grouping and the null, it should match.

    Login or Signup to reply.
  2. I must say I am unfamiliar with the exact notation you are using. You appear to be using some parser that has the embedded regex matching in it, rather than having a regex that matches the entire JSON string. It would appear that everything outside of your dollar-braced unit is being interpreted literally in which case you need to not have the double quotes when you are matching the null expression. So, depending upon how your parser handles the embedded quotes in the regex, you either want

    {"name":${json-unit.matches:^("[A-Za-z0-9]+"|null)$}}
    

    or

    {"name":${json-unit.matches:^(\"[A-Za-z0-9]+\"|null)$}}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search