Removing white spaces within json vaule

Viewed 1758

I have a string which stores webservice response as below. In this string I want to remove the whitespace next to the double quotes(opening & closing" using java.

String WebserviceResponse;

    {
        "assessmentID": 289,
        "assesmentName": " OneCert ",
        "accountName": " spadmin ",
        "assetName": " OneCert ",
        "applicationName": " OneCert ",
        "accountType": " LocalAccount ",
        "environment": " Test "
    },
    {
        "assessmentID": 289,
        "assesmentName": "OneCert ",
        "accountName": "9000032",
        "assetName": "OneCert",
        "applicationName": "OneCert ",
        "accountType": "zone2.scbdev.net",
        "environment": "Test"
    }

I tried the below code to remove space next to double quotes String FormattedResponse= WebserviceResponse.replaceAll( "\s","" );

but it removes all the spaces. Can you please help me on this.

2 Answers

If you don't want to parse the JSON, you can use this regex: (?>\\S)(\\s)(?>\") [updated below] and replace the matches with "" (empty string).

Breaking this up:

(?>\\S) non-capturing group, looking for anything that is NOT a whitespace

(\\s) a capturing group looking for a whitespace

(?>\") a non-capturing group, looking for " (quotes)

You'll have whitespaces matches. Then is just replaceAll() with "" (empty string).

Result here: Regex101

UPDATE

I did some tests and I had some issues with non-capturing group and java. It may be better to use lookahead/lookbehind. I'm updating the answer and posting an example here.

This is the lookahead/lookbehind version of the regex: (?<=\")(\\s)(?=\\w)|(?<=\\w)(\\s)(?=\"). Regex101

Example in Java

" \" assesmentName\": \" OneCert \"".replaceAll("(?<=\")(\\s)(?=\\w)|(?<=\\w)(\\s)(?=\")", "")

resulting in:

"assesmentName": "OneCert"

Breaking up

(?<=\")(\\s)(?=\\w) look for quote, space, word (letter or number); or

(?<=\\w)(\\s)(?=\") look for a word (letter or number), space, quote

Please note: The following example probably only works on basic JSON strings. For better results you should use some lib to decode, filter and encode String again.

You could use some regexp on the string, something like this:

Thanks to: https://regex101.com/r/FVPbD0/4

"([^"]+)":\s*"\s*([^"]+)\s*"
  • Roughly, this extracts the key and value without spaces into groups
  • and replaces them without the spaces

Java code:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

final String regex = "\"([^\"]+)\":\\s*\"\\s*([^\"]+)\\s*\"";
final String string = " {\n"
     + "        \"assessmentID\": 289,\n"
     + "        \"assesmentName\": \" OneCert \",\n"
     + "        \"accountName\": \" spadmin \",\n"
     + "        \"assetName\": \" OneCert \",\n"
     + "        \"applicationName\": \" OneCert \",\n"
     + "        \"accountType\": \" LocalAccount \",\n"
     + "        \"environment\": \" Test \"\n"
     + "    },\n"
     + "    {\n"
     + "        \"assessmentID\": 289,\n"
     + "        \"assesmentName\": \"OneCert \",\n"
     + "        \"accountName\": \"9000032\",\n"
     + "        \"assetName\": \"OneCert\",\n"
     + "        \"applicationName\": \"OneCert \",\n"
     + "        \"accountType\": \"zone2.scbdev.net\",\n"
     + "        \"environment\": \"Test\"\n"
     + "    }";
final String subst = "\"$1\": \"$2\"";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);

System.out.println("Substitution result: " + result);
Related