0

I am trying to validate a JSON string using regex. Found the valid regex from another post https://stackoverflow.com/a/3845829/7493427 It uses DEFINE feature in regex. But I think the JRegex library does not support that feature. Is there a work around for this?

I used java.util.regex first, then found out about JRegex library. But this doesn't work too.

String regex = "(?(DEFINE)" +
"(?<number>   -? (?= [1-9]|0(?!\\d) ) \\d+ (\\.\\d+)? ([eE] [+-]? 
\\d+)? )" +
"(?<boolean>   true | false | null )" +
"(?<string>    \" ([^\"\\n\\r\\t\\\\\\\\]* | \\\\\\\\ 
[\"\\\\\\\\bfnrt\\/] | \\\\\\\\ u [0-9a-f]{4} )* \" )" +
"(?<array>     \\[  (?:  (?&json)  (?: , (?&json)  )*  )?  \\s* 
\\] )" +
"(?<pair>      \\s* (?&string) \\s* : (?&json)  )" +
"(?<object>    \\{  (?:  (?&pair)  (?: , (?&pair)  )*  )?  \\s* 
\\} )" +
"(?<json>   \\s* (?: (?&number) | (?&boolean) | (?&string) | (? 
&array) | (?&object) ) \\s* )" +
")" +
"\\A (?&json) \\Z";
String test = "{\"asd\" : \"asdasdasdasdasdasd\"}";
jregex.Pattern pattern = new jregex.Pattern(regex);
jregex.Matcher matcher = pattern.matcher(test);
if(matcher.find()) {
    System.out.println(matcher.groups());
}

I expected a match as the test json is valid, but I get an exception.

Exception in thread "main" jregex.PatternSyntaxException: unknown group name in conditional expr.: DEFINE at jregex.Term.makeTree(Term.java:360) at jregex.Term.makeTree(Term.java:219)at jregex.Term.makeTree(Term.java:206) at jregex.Pattern.compile(Pattern.java:164) at jregex.Pattern.(Pattern.java:150) at jregex.Pattern.(Pattern.java:108) at com.cloak.utilities.regex.VariableValidationHelper.main(VariableValidationHelper.java:305)

pushkin
  • 9,575
  • 15
  • 51
  • 95
Ashwath
  • 61
  • 7
  • Possible duplicate of [Using regex to parse a very simple JSON file](https://stackoverflow.com/questions/20544646/using-regex-to-parse-a-very-simple-json-file) – Lino May 01 '19 at 11:19
  • 1
    Using regex to "parse" JSON is a very very very bad idea. You better use a dedicated parser. – Lino May 01 '19 at 11:20
  • I am VALIDATING a JSON string using REGEX and not trying to parse it. Kindly read the question properly. – Ashwath May 01 '19 at 11:22
  • Parsing includes validation, so using a parser like Jackson will likely work without a problem – Lino May 01 '19 at 11:23
  • I think that takes a lot more computation. All I want is validate and find a match. Kindly look into this link https://stackoverflow.com/a/3845829/7493427 . I am using the same regex, but getting an exception. – Ashwath May 01 '19 at 11:26
  • 1
    From the linked post: *Most modern regex implementations allow for recursive regexpressions [..]* -> [java doesn't allow recursive regex](https://stackoverflow.com/a/8660012/5515060) – Lino May 01 '19 at 11:28

1 Answers1

0

You can use this rather simple Jackson setup:

private static final ObjectMapper MAPPER = new ObjectMapper();

public static boolean isValidJson(String json) {
    try { 
        MAPPER.readValue(json, Map.class);
        return true;
    } catch(IOException e) {
        return false;
    }
}

ObjectMapper#readValue() will throw JsonProcessingExceptions (a sub class of IOException) when the input is invalid.

Lino
  • 19,604
  • 6
  • 47
  • 65
  • I am aware about this solution. But I want to use REGEX as there is a restriction for me. As per your comment https://github.com/florianingerl/com.florianingerl.util.regex library supports regex recursion in java and I will be using it. Thanks for the help – Ashwath May 01 '19 at 12:06