'Regex to not accept invalid characters except JSON braces and colon [closed]

I need to set up a validation rule for request params using RegEx. The rule should not allow invalid characters except json's ("{}", "[]", ":").

For example:

    "Piece" -> correct
    
    "Piece$%" -> incorrect
    
    {"labelName": "myName"} -> correct

However, my RegEx does not pass json format inputs.

    [a-zA-Z0-9\s{}:\[]]+


Solution 1:[1]

You could achieve your check by negating the logic. Instead of looking for the characters allowed, you could look for any character which is not allowed. If your regex finds any match then the json is not consistent.

This regex looks for anything that is not a word character (letter or number), a white space or a json's symbol ([, ], {, }, :, " or a comma)

[^\w\s\{\}\[\]\:\",]

Here there is a link to test the regex.

https://regex101.com/r/7pn2FD/2

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1