convert jsonString contains special characters to jsonObject

93 views Asked by At

I have jsonString which contains special characters.I want to remove these special characters to obtain proper jsonString then jsonObject. My code is :

String text="\"\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"createdAt\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"2023-10-31T11:43:29\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"updatedAt\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"2023-10-31T11:43:30\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"createdBy\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":null,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"updatedBy\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\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\\\\\\\\\\\\\\\\\\\\\\"priority\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":1\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n}\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n}\\\\\\\"\\\"\"";

Do you have any idea how can i convert inproper string to proper jsonObject.

I have tried replace all method but I still see some characters :(

What i have done until now:

text.replace("\\","").replace("\\n","UUU").replace("\\n",TTT").replace("\"\\\"\\\"\\\"\\\"{","\"{")
1

There are 1 answers

2
Sree Kumar On BEST ANSWER

Writing down here only to show how to analyze such issues and design a solution. The solution being presented here is only for the particular string pasted and any that have similar extra characters.

Approach:

  1. Study the string to know the included patterns.
  2. Create patterns to remove or replace them in the right order.

Here is a class that achieves it for the string in question. I have added some comments to explain the approach.

public class SOCleanupJSONString {
    public static void main(String[] args) {
        String text="\"\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"createdAt\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"2023-10-31T11:43:29\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"updatedAt\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\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\\\\\\\\\\\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"status\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"CREATED\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"priority\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":1\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n}\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n}\\\\\\\"\\\"\"";

        /* 1. Extract the JSON block from the string. 
           NOTE: This pattern is only for object blocks. You may have array blocks as well. Please change this pattern for that. */
        Pattern JSON_BLOCK = Pattern.compile( "\\{.*}" );
        Matcher m = JSON_BLOCK.matcher( text );
        if( m.find() ) {
            text = m.group();
            System.out.println(text);

            /* 2. Un-escape quotes */
            text = text.replaceAll("\\\"", "\"");

            /* 3. Remove (JSON escaped) newline text */
            text = text.replaceAll("\\\\n", "" );

            /* 4. Remove all extra double-slashes.
             *    NOTE: This can be erroneous, if the pattern appears within a value. */
            text = text.replaceAll("\\\\", "" );
            System.out.println(text);
        }
        else System.out.println( "No JSON block found" );

        parse( text );

    }

    private static void parse( String text ){
        ObjectMapper om = new ObjectMapper();
        try {
            Map map = om.readValue( text, Map.class );
            System.out.println( map );
        }
        catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

    }
}

Resultant JSON string:

{"createdAt":"2023-10-31T11:43:29","updatedAt":"2023-10-31T11:43:30","createdBy":null,"updatedBy":null,"isDeleted":false,"messageValue":{"name":"hello world","status":"CREATED","priority":1}}

Output of Map.toString() after unmarshalling the above to a Map instance using Jackson:

{createdAt=2023-10-31T11:43:29, updatedAt=2023-10-31T11:43:30, createdBy=null, updatedBy=null, isDeleted=false, messageValue={name=hello world, status=CREATED, priority=1}}