0

I have a malformed json array string which I get from an API call as follows:

[{\"ResponseCode\":1,\"ResponseMsg\":\"[{\"Code\":\"CA2305181\",\"Message\":\"Processed successfully\"}]\"}]

There is a double quote before open square bracket in the value of Response Msg property. Is there a way to convert this into Java object ?

What I have tried so far: I have used Jackson to parse it as follows but it gives error

ObjectMapper mapper = new ObjectMapper();
            mapper.setPropertyNamingStrategy(new ResponseNameStrategy());
            Response[] response = mapper.readValue(strOutput1, Response[].class);
Error: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token

I have also tried using Gson to parse it but it also gives error

Gson gson = new GsonBuilder()
                    .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
                    .create();
            Response[] response = gson.fromJson(strOutput1, Response[].class);
Error: Expected BEGIN_ARRAY but was STRING at line 1 column 35 path $[0].ResponseMsg

I have gone through the following links on StackOverflow but none of them has addressed my issue:

How to Convert String Array JSON in a Java Object

Convert a JSON string to object in Java ME?

JSON Array to Java objects

Convert json String to array of Objects

converting 'malformed' java json object to javascript

A_J
  • 977
  • 4
  • 16
  • 44
  • I think it's time to go back to that API and figure out why it is sending out broken JSON. If it's their fault, then no one can consume their service. The alternative is that you did something to the data possibly when you received it. Maybe tell us exactly how you got this broken JSON data. – Tim Biegeleisen May 25 '18 at 06:37
  • Your question does not make sense to me atleast. When something is not a valid JSON it cannot be called a JSON. It is just a string. – wandermonk May 25 '18 at 06:41
  • I got this json as part of Soap Web Service Response as follows: [{"ResponseCode":1,"ResponseMsg":"[{\"Code\":\"CA2305181\",\"Message\":\"Processed successfully.\"}]"}] – A_J May 25 '18 at 06:43
  • First of all, the service should be fixed right away. Also, i think there is another extra double quote at the end }]\"}]. That double quote is an error too? – Rcordoval May 25 '18 at 06:45
  • That is the closing double quote of the one starting before the square bracket – A_J May 25 '18 at 06:50
  • What was that comment with the SOAP response supposed to mean, A_J? Why is that string different from the one in your original question. It looks like the JSON from the SOAP simply contains another JSON object as an escaped string and it has been broken on your end with some kind of incomplete extra escaping. – Torben May 25 '18 at 06:51
  • @A_J the JSON you wrote in your comment is correct, not the one in your question. – Maurice Perry May 25 '18 at 06:52
  • Both the json are same I have updated the question. I am getting the json as string. That's why I had put double quotes in the beginning and end – A_J May 25 '18 at 06:57
  • The response in your comments is valid. – Rcordoval May 25 '18 at 06:57
  • There is double quotes before the square bracket in Response Msg value. That is causing the problem "ResponseMsg":"[{\"Code. Please read the question carefully before commenting – A_J May 25 '18 at 06:59
  • Why dont you get the responseMsg as a string, and then that parse it to json?, in the end, you only need response i guess – Rcordoval May 25 '18 at 07:04
  • Please give the reason for downvoting. I have researched well before asking the question here. This is like discouraging people from asking questions here – A_J May 25 '18 at 07:06
  • @Rcordoval , How can I get ResponseMsg as string ? Please help – A_J May 25 '18 at 07:34
  • I don't have control over what the API is returning as it is a third party API – A_J May 25 '18 at 07:40

3 Answers3

2

I think the answer is in the comments, you appear to be trying to solve the issue on the wrong place.

You are receiving json which you wish to parse into java objects, unfortunately the json is malformed so will not parse.

As a general rule you should never be trying to solve the symptom, but should look for the root cause and fix that, it may sound trivial but fixing symptoms leads to messy, unpredictable, and unmaintainable systems.

So the answer is fix the json where it is being broken. If this is something or of your control, while you wait for the fix, you could put a hack in to fix the json before you parse it.

This way you won't compromise your parsing, and only have a small piece of string replacement to remove when the third party has fixed the issue. But do not go live with the hack, it should only be used during development.

MartinByers
  • 1,240
  • 1
  • 9
  • 15
  • I don't have a control over what the API is returning. And as mentioned in the question I am unable to parse it. I have even tried to parse it using JSONArray jsonArray = new JSONArray(strOutput1); still getting the error Expected a ',' or '}' at 38 [character 39 line 1] – A_J May 25 '18 at 07:32
1

As i mentioned in the comment, you should prepare your service response in order to parse it.

I implemented an example:

public class JsonTest {
    public static void main(String args[]) throws JsonProcessingException, IOException{
        String rawJson = 
                "[{\"ResponseCode\":1,\"ResponseMsg\":\"[{\"Code\":\"CA2305181\",\"Message\":\"Processed successfully\"}]\"}]";
        String goodJson = "{"+rawJson.split("[{{.}]")[2]+"}";
        ObjectMapper mapper = new ObjectMapper();
        final ObjectNode node = mapper.readValue(goodJson, ObjectNode.class);
        System.out.println("Pretty Print: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node));

        System.out.println("Just code: " + node.get("Code"));   
    }

}

Which returns:

output

Rcordoval
  • 1,932
  • 2
  • 19
  • 25
  • In split method, single start curly brace in character class will also achieve the same result – A_J May 25 '18 at 11:27
0

This is how I finally solved my issue:

String inputJsonStr = "[{\"ResponseCode\":1,\"ResponseMsg\":\"[{\"Code\":\"CA2305181\",\"Message\":\"Claim has been added successfully.\"}"
                + "]\"}]";
int indexOfRes = inputJsonStr.indexOf("ResponseMsg");

        if(inputJsonStr.substring(indexOfRes+13,indexOfRes+14).equals("\""))
        {
            inputJsonStr = inputJsonStr.substring(0,indexOfRes+13) + inputJsonStr.substring(indexOfRes+14);
        }
        int indexOfFirstClosingSquare = inputJsonStr.indexOf("]");
        if(inputJsonStr.substring(indexOfFirstClosingSquare+1, indexOfFirstClosingSquare+2).equals("\"")) {
            inputJsonStr = inputJsonStr.substring(0, indexOfFirstClosingSquare+1)+inputJsonStr.substring(indexOfFirstClosingSquare+2);
        }

Now inputJsonStr contains a valid json array which can be parsed into Java custom object array easily with gson as given in this SO link:

Convert json String to array of Objects

A_J
  • 977
  • 4
  • 16
  • 44