0

I have not been able to find a solution to this online. I have the following JSON.

[{
 "Name":"Adam",
"Age":"29",
"Addr":"Roland st",
"Height":"180",
"Kids": ["Charlie"]
"DummyData":
[{
    "DummyData1":null,
    "DummyData2":"840",
    "DummyData3":
    [{
        "DummyData4":"INQU",
        "DummyData5":null,
        "DummyData6":null,
        "DummyData7":123.45,
    }]
}]  }]

This JSON structure may have different attributes in the future. Likewise it might also have different null values including in the Kids element (can have multiple single elements or null e.g., "Kids": ["Name", ""]). The above is just an example.

I want to replace all Null values with empty strings, and then return the JSON Array as a String.

This is what I have so far.

private void traverse(JSONArray jsonArray) {

    for (int i = 0; i < jsonArray.length(); i++) {

        if(jsonArray.optJSONObject(i) != null) { //is null at "Kids": ["Charlie"]

            JSONObject objects = jsonArray.getJSONObject(i);
            Iterator<String> key = objects.keys();

            while (key.hasNext()) {
                String k = key.next().toString();
                Object innerObject = objects.get(k);

                if (innerObject instanceof JSONArray) {
                    JSONArray arr = objects.getJSONArray(k);
                    traverse(arr);

                } 
                else if (!objects.isNull(k)) {
                    if (innerObject instanceof Integer) {
                        System.out.println("Key : " + k + ", value : " + objects.getInt(k));}
                    else if (innerObject instanceof String) {
                        System.out.println("Key : " + k + ", value : " + objects.getString(k));
                    } else if (innerObject instanceof Boolean) {
                        System.out.println("Key : " + k + ", value : " + objects.getBoolean(k));
                    } else if (innerObject instanceof Double) {
                        System.out.println("Key : " + k + ", value : " + objects.getDouble(k));
                    } else if (innerObject instanceof Long) {
                        System.out.println("Key : " + k + ", value : " + objects.getLong(k));
                    }
                } 
                else {
                    System.out.println("NULL value found!!!");
                    
                }
            }
            System.out.println("-----------");
        }
    }
}

As you can see I have already been able to traverse the entire json tree. However I have not been able to figure out how I can replace the null values in such a way that the structure of the json does not change? Help would be appreciated. Thanks in advance.

code-monkey
  • 236
  • 1
  • 2
  • 11
  • If json is coming as string, use regex – bananas Jan 21 '22 at 17:49
  • @bananas Can't be harder than to [parse XML with regex](https://stackoverflow.com/a/1732454/845414), right? Really: Use a streaming JSON parser. Replace all occurrences of null with the empty string and pass everything to a JSON writer. – Johannes Kuhn Jan 21 '22 at 17:57

0 Answers0