2

I am thinking of a best solution to replace all the whitespaces in JSON keys with underscore.

{ 
  "Format": "JSON", 
  "TestData": { 
    "Key with Spaces in it": { 
      "And Again": { 
        "Child_Key1": "Financial", 
        "Child_Key2": null 
      }, 
......... 
..... 

I want the above to be converted as shown below:

{ 
  "Format": "JSON", 
  "TestData": { 
    "Key_with_Spaces_in_it": { 
      "And_Again": { 
        "Child_Key1": "Financial", 
        "Child_Key2": null 
      }, 
......... 
..... 

Any suggestions ?

Does any Java library have any predefined function to do this ?

1

6 Answers 6

8

Replacing Keys

The following code uses Google's JSON parser to extract keys, reformat them, and then create a new JSON object:

public static void main(String[] args) {
    String testJSON = "{\"TestKey\": \"TEST\", \"Test spaces\": { \"child spaces 1\": \"child value 1\", \"child spaces 2\": \"child value 2\" } }";
    Map oldJSONObject = new Gson().fromJson(testJSON, Map.class);
    JsonObject newJSONObject = iterateJSON(oldJSONObject);

    Gson someGson = new Gson();
    String outputJson = someGson.toJson(newJSONObject);
    System.out.println(outputJson);
}

private static JsonObject iterateJSON(Map JSONData) {
    JsonObject newJSONObject = new JsonObject();
    Set jsonKeys = JSONData.keySet();
    Iterator<?> keys = jsonKeys.iterator();
    while(keys.hasNext()) {
        String currentKey = (String) keys.next();
        String newKey = currentKey.replaceAll(" ", "_");
        if (JSONData.get(currentKey) instanceof Map) {
            JsonObject currentValue = iterateJSON((Map) JSONData.get(currentKey));
            newJSONObject.add(currentKey, currentValue);
        } else {
            String currentValue = (String) JSONData.get(currentKey);
            newJSONObject.addProperty(newKey, currentValue);
        }
    }
    return newJSONObject;
}

You can read more about GSON here.

Replacing Values

Depending on how your JSON data is set up, you might need to switch JSONArray with JSONObject.

JSONArrays begin and end with [], while JSONObjects begin and end with {}

In short, these methods will travel over an entire array/object and replace any spaces with underscores. They're recursive, so they will dive into child JSONArrays/JSONObjects.

If the JSON data is encoded as a Java JSONArray, you can do the following:

public static void removeJSONSpaces(JSONArray theJSON) {
    for (int i = 0; while i < theJSON.length(); i++) {
        if (theJSON.get(i) instanceof JSONArray) {
            currentJSONArray = theJSON.getJSONArray(i);
            removeJSONSpaces(currentJSONArray);
        } else {
            currentEntry = theJSON.getString(i);
            fixedEntry = currentEntry.replace(" ", "_");
            currentJSONArray.put(i, fixedEntry);
        }
    }
}

In short, this method will travel over an entire array and replace any spaces with underscores. It's recursive, so it will dive into child JSONArrays.

You can read more about JSONArrays here

If the data is encoded as a JSONObject, you'd want to do something like:

public static void removeJSONSpaces(JSONObject theJSON) {

    jObject = new JSONObject(theJSON.trim());
    Iterator<?> keys = jObject.keys();

    while(keys.hasNext()) {
        String key = (String)keys.next();
        if (jObject.get(key) instanceof JSONObject) {
            removeJSONSpaces(jObject.get(key))
        } else {
            currentEntry = theJSON.getString(i);
            fixedEntry = currentEntry.replace(" ", "_");
            currentJSONArray.put(i, fixedEntry);
        }
    }

}

You can read more about JSONObjects here

5
  • I really appreciate your response, Your idea looks good. But I want to rename the name of the key itself, how a put() will rename the key ? Wont it become a value ? I am bit confused here.
    – gnanagurus
    May 14, 2015 at 5:19
  • Also this will only replace the extreme child keys, not all the keys :(
    – gnanagurus
    May 14, 2015 at 5:27
  • Man, Any more comments on this ? Your thoughts could trigger a better solution here :)
    – gnanagurus
    May 15, 2015 at 12:08
  • Howdy, as I mentioned put is not the exact solution. But I had tweaked your solution a but. In both key and value, I am replacing space with SP, symbols with SYMBOL_KEYWORD . Then that resolves my problem. As I have a external response mapping to do, I am using xslt to replace this with actual value, for example SP with ' '
    – gnanagurus
    May 19, 2015 at 1:19
  • 1
    the type names have changed slightly in gson 2.8.2 - JSONArray is now JsonArray & JSONObject is now JsonObject Feb 8, 2018 at 11:59
1

You can just read in each of the keys/values then use the String replace(String oldString, String newString) method.

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(char,%20char)

1

Try this one:

      String testJSON = "{\"menu\": {\n" +
        "  \"id no\": \"file\",\n" +
        "  \"value type\": \"File\",\n" +
        "  \"popup value\": {\n" +
        "    \"menu item\": [\n" +
        "      {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},\n" +
        "      {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},\n" +
        "      {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}\n" +
        "    ]\n" +
        "  }\n" +
        "}}";
             JSONObject json = new JSONObject(testJSON);
             System.out.println("Final output: "+replaceKeyWhiteSpace(json));

replace the WhiteSpace with "_"

private static JSONObject replaceKeyWhiteSpace(Object json) {
JSONObject jsonObject = null;
if (json instanceof JSONObject) {
    jsonObject = (JSONObject) json;
    List<String> keyList = new LinkedList<String>(jsonObject.keySet());
    for (String key : keyList) {
        if (!key.matches(".*[\\s\t\n]+.*")) {
            Object value = jsonObject.get(key);
            replaceKeyWhiteSpace(value);
            continue;
        }

        Object value = jsonObject.remove(key);
        String newKey = key.replaceAll("[\\s\t\n]", "_");

        replaceKeyWhiteSpace(value);

        jsonObject.accumulate(newKey, value);

    }
} else if (json instanceof JSONArray) {
    for (Object aJsonArray : (JSONArray) json) {
        replaceKeyWhiteSpace(aJsonArray);
    }
}
return jsonObject;

}

0

I am using gson library in order to remove bad keys from json string because I want to create the AVRO file of the json. Keys with spaces or non Alphanumeric are not acceptable from Avro so I have to sanitize input first.

The above snippet removes whitespaces and character $ from all keys (iterative). I assume my json string is JsonObject.

Check out a small sample code that I have used.

for (String json: allJsons){
        JsonObject schema = new JsonParser().parse(json).getAsJsonObject();
        JsonObject cleanSchema = new JsonObject();
        Set<Map.Entry<String, JsonElement>> entries = schema.entrySet();
        for (Map.Entry<String, JsonElement> entry: entries) {
            String key = entry.getKey().replace("$","").replace(" ","");
            JsonElement value = entry.getValue();
            if (value.isJsonObject()){
                JsonObject subValue = new JsonObject();
                Set <Map.Entry<String, JsonElement>> subEntries = ((JsonObject) value).entrySet();
                for (Map.Entry<String, JsonElement> subEntry: subEntries) {
                    String subKey = subEntry.getKey().replace("$","").replace(" ","");
                    subValue.add(subKey, subEntry.getValue());
                }
                value = subValue;
            }
            cleanSchema.add(key, value);
        }

        System.out.println("Clean schema "+cleanSchema.toString());
        byte[] bytes = converter.convertToAvro(cleanSchema.toString().getBytes(), avroSchema);
        String avroFile = outputFileName+"_"+counter+".avsc";
        Files.write(Paths.get(avroFile), bytes);
        counter++;
        System.out.println("Avro File Created "+avroFile);
    }
0

This is what I did to remove white space from Json Keys. Solution will work for JsonArray and JsonObject, just printing matching regex keys with white space and replace those keys. Live Demo : online Java compiler

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

public class MatcherGroupExample {

    public static void main(String[] args) {

        String text   = "{\"co nf ig\": {\"oauthSecret\": [{\"i d\": 45,\"config123\": {\"oauthSecret\": \"P8n2x5Ht0nFRRB0A\",\"status\": \"CREATED\"},\"SERVER132\": \"1000\"},{\"id\": 46,\"config123\": {\"oauthSecret\": \"wP8n2x5Ht0nFRRB0A\",\"status\": \"CREATED\"},\"SERVER132\": \"1000\"}],\"oauthKey\": \"newtest\",\"SERV ER\": \"1000\"},\"feat ures\": [ 9004, 9005] ,\"d\":\"dd\"}";


        String patternString1 = "\"([^\"]+?[ ]+[^\"]+?)\"\\s*:";

        Pattern pattern = Pattern.compile(patternString1);
        Matcher matcher = pattern.matcher(text);

        while(matcher.find()) {
            System.out.println("found: " + matcher.group(1));
           text= text.replaceAll("\""+matcher.group(1)+"\"","\""+(matcher.group(1).replaceAll(" ",""))+"\"");

        }
         System.out.println("Final output:"+text);
    }
}
1
  • How do I trim for json values using this code Nov 26, 2021 at 4:22
-2

Remove only the white space on the strings in jason code not all white space on jason code

public String removeWhiwspaceInJasonString(String jsonStr) {
    boolean changeActive = false;
    String output = '';
    for(int i=0; i<jsonStr; i++){

        if(jsonStr.charAt(i) == '"')
            changeActive  =!changeActive;
        else if(changeActive)
          if(jsonStr.charAt(i) == ' ')
             output += '_';
          else
             output += jsonStr.charAt(i);
        else
          output += jsonStr.charAt(i);

    }
    return output;
}
1
  • 1
    Manuel string or regular expresion operations are highly discouraged for manpulating JSON strings. Due to specifications there are many data, function formats along with escape char rules.
    – hsnkhrmn
    Jun 5, 2015 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.