Pages

Thursday, February 18, 2016

Java JSON Parser Example with Regex



Recently I came across with problem where I needed to recognize and parse custom exception message inside JSON on Java server side. For that particular problem I used Regex.

The JSON response format is like this:

json-with-exception-message

And this is the code:

protected void checkException(String response) throws ApiException {
        
    String errorCode = null;
    String message = null;
    Map<String, String> properties = null;

    Pattern errorCodePattern = Pattern.compile("\"code\"\\s*:\\s*\"([^,]*)\",");
    Pattern messagePattern = Pattern.compile("\"message\"\\s*:\\s*\"([^,]*)\",");
    Pattern statusPattern = Pattern.compile("\"status\"\\s*:\\s*\"(FAILURE)\"");        

    Matcher errorCodeMatcher = errorCodePattern.matcher(response);
    Matcher messageMatcher = messagePattern.matcher(response);
    Matcher statusMatcher = statusPattern.matcher(response);        

    boolean errorCodeFlag = errorCodeMatcher.find();
    boolean messageFlag = messageMatcher.find();
    boolean statusFlag = statusMatcher.find();                

    if (errorCodeFlag && messageFlag && statusFlag) {
        
        Pattern propertiesPattern = Pattern.compile("\"properties\"\\s*:\\s*\\[(\\{[^\\]]*)");
        Pattern propertiesValuePattern = Pattern.compile("\"value\"\\s*:\\s*\"([^\"]*)\"");
        Pattern propertiesKeyPattern = Pattern.compile("\"key\"\\s*:\\s*\"([^\"]*)\"");

        Matcher propertiesMatcher = propertiesPattern.matcher(response);
        
        boolean propertiesFlag = propertiesMatcher.find();
        
        boolean keyFlag = false;
        boolean valueFlag = false;
        
        errorCode = errorCodeMatcher.group(1);
        message = messageMatcher.group(1);

        System.out.println("\nJSON response contains exception:\n");
        System.out.println("code:        " + errorCode);
        System.out.println("message:     " + message);
        System.out.println("status:      " + statusMatcher.group(1));

        if (propertiesFlag) {

            properties = new HashMap<String, String>();
            String propertiesData = propertiesMatcher.group(1);

            Matcher keyMatcher = propertiesKeyPattern.matcher(propertiesData);
            Matcher valueMatcher = propertiesValuePattern.matcher(propertiesData);

            keyFlag = keyMatcher.find();
            valueFlag = valueMatcher.find();

            System.out.println("properties:    ");
            if (keyFlag && valueFlag && keyMatcher.groupCount() == valueMatcher.groupCount()) {
                for (int i = 1; i <= keyMatcher.groupCount(); i++) {
                    System.out.println("key: " + keyMatcher.group(i) + "  value: " + valueMatcher.group(i));
                    properties.put(keyMatcher.group(i), valueMatcher.group(i));
                }
            }
            System.out.println("");
        }
        throw new ApiException(errorCode, message, properties);
    }
}
The output:
json-parser-output

No comments:

Post a Comment