'Parsing json with different nesting with escapes in JAVA

"{\"statusCode\":400,\"throttle\":{\"retryAfter\":null,\"intRetryAfter\":0,\"throttled\":false},\"code\":\"FAILED\",\"description\":\"{\\\"statusCode\\\":400,\\\"code\\\":\\\"FAILED\\\",\\\"description\\\":\\\"\\\\\\\"{ \\\\\\\\\\\\\\\"response\\\\\\\\\\\\\\\":[{\\\\\\\\\\\\\\\"code\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"SUCCESS\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"description\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"SUCCESSFUL MESSAGE\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"Id\\\\\\\\\\\\\\\":123},{\\\\\\\\\\\\\\\"code\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"NOT_FOUND\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"description\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"FAILED MESSAGE\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"errors\\\\\\\\\\\\\\\":[{\\\\\\\\\\\\\\\"Error\\\\\\\\\\\\\\\":{\\\\\\\\\\\\\\\"message\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"Id not found\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"reason\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"ID_NOT_FOUND\\\\\\\\\\\\\\\"}}],\\\\\\\\\\\\\\\"Id\\\\\\\\\\\\\\\":1234}]}\\\\\\\"\\\",\\\"throttle\\\":{\\\"retryAfter\\\":null,\\\"intRetryAfter\\\":0,\\\"throttled\\\":false}}\",\"adId\":0}"

I am receiving a response from a network call, which contains an array with a combination of success and failure responses, I would like to extract them out from this escaped JSON. In the above example I got two code one is a SUCCESS CODE and other is a FAILED CODE, I would like to extract them out and map to a common class.

I tried parsing this JSON using StringEscapeUtils.unescapeJava(stringToBeParsed), but I am still not able to map all the fields, is there a more efficient way to do it? where I can extract response array and map individual field to a common class ?

PS: I am new to java, and not sure about an efficient way to do this here. Writing my own parser would be an overkill to this problem I feel.



Sources

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

Source: Stack Overflow

Solution Source