1

Im trying to restructure duplicate json values in my JSON Structure and rearrange them using the simplest possible method.

I got to the point where I managed to get it stored in a map each time it loops over the JSONObject but from here how do I proceed to store the mapping to achieve my desired outcome? Thank you so much in advance.

public static void main(String[] args) throws JSONException  {

    String jsonString = "[{\"file\":[{\"fileRefNo\":\"AG/CSD/1\",\"status\":\"Active\"}],\"requestNo\":\"225V49\"},{\"file\":[{\"fileRefNo\":\"AG/CSD/1\",\"status\":\"Inactive\"}],\"requestNo\":\"225SRV\"},{\"file\":[{\"fileRefNo\":\"AG/CSD/2\",\"status\":\"Active\"}],\"requestNo\":\"225SRV\"}]" ;

      JSONArray json = new JSONArray(jsonString);

      Map<String, Object> retMap = new HashMap<String, Object>();

      for (int i = 0; i < json.length(); i++ ) {
        if(json != JSONObject.NULL) {
            retMap = toMap(json.getJSONObject(i));
            System.out.println(retMap + "retMap");
            //{file=[{fileRefNo=AG/CSD/1, status=Active}], requestNo=225V49}retMap
            //{file=[{fileRefNo=AG/CSD/1, status=Inactive}], requestNo=225SRV}retMap
            //{file=[{fileRefNo=AG/CSD/2, status=Active}], requestNo=225SRV}retMap
        }
      }
}
public static Map<String, Object> toMap(JSONObject object) throws JSONException {
    Map<String, Object> map = new HashMap<String, Object>();

    Iterator<String> keysItr = object.keys();
    while(keysItr.hasNext()) {
        String key = keysItr.next();
        Object value = object.get(key);

        if(value instanceof JSONArray) {
            value = toList((JSONArray) value);
        }
        else if(value instanceof JSONObject) {
            value = toMap((JSONObject) value);
        }
        map.put(key, value);
    }
    return map;
}

public static List<Object> toList(JSONArray array) throws JSONException {
    List<Object> list = new ArrayList<Object>();
    for(int i = 0; i < array.length(); i++) {
        Object value = array.get(i);

        if(value instanceof JSONArray) {
            value = toList((JSONArray) value);
        }
        else if(value instanceof JSONObject) {
            value = toMap((JSONObject) value);
        }
        list.add(value);
    }
    return list;
}

Here is my initial JSONArray

[{
    "file": [{
            "fileRefNo": "AG/CSD/1",
            "status": "Active"
        }],
    "requestNo": "225V49"
}, {
    "file": [{
            "fileRefNo": "AG/CSD/1",
            "status": "Inactive"
        }],
    "requestNo": "225SRV"
}, {
    "file": [{
            "fileRefNo": "AG/CSD/2",
            "status": "Active"
        }],
    "requestNo": "225SRV"
}]

Here is my desired outcome

[{
    "file": [{
            "fileRefNo": "AG/CSD/1",
            "status": "Active"
        }],
    "requestNo": "225V49"
}, {
    "file": [{
            "fileRefNo": "AG/CSD/1",
            "status": "Inactive"
        },{
            "fileRefNo": "AG/CSD/2",
            "status": "Active"
        }],
    "requestNo": "225SRV"
}]
Roth
  • 83
  • 12
  • You can try converting your original JSON object to a Map, iterate over the map and aggregate the file entities as you described in the OP, and then create a new JSON object based on your aggregated map. See the following for converting a JSON object to a Map: https://stackoverflow.com/questions/21720759/convert-a-json-string-to-a-hashmap – Rann Lifshitz May 01 '19 at 10:23
  • I transformed it into a map and this the output is shown above. From there on do I reiterate the out map or do I create a new JSON in the process of reiterating the map? – Roth May 01 '19 at 13:01
  • I think it would be simpler to create a new map and output this map as a JSON object. – Rann Lifshitz May 01 '19 at 13:12
  • ok,but how do I reiterate when it comes to duplicates like the above scenario? – Roth May 01 '19 at 13:24
  • Simple - your new Map should use the value of `requestNo` as the key and the `file` array as the mapped value. – Rann Lifshitz May 01 '19 at 19:43

1 Answers1

1

https://github.com/octomix/josson

Josson josson = Josson.fromJsonString(
    "[{" +
    "    \"file\": [{" +
    "            \"fileRefNo\": \"AG/CSD/1\"," +
    "            \"status\": \"Active\"" +
    "        }]," +
    "    \"requestNo\": \"225V49\"" +
    "}, {" +
    "    \"file\": [{" +
    "            \"fileRefNo\": \"AG/CSD/1\"," +
    "            \"status\": \"Inactive\"" +
    "        }]," +
    "    \"requestNo\": \"225SRV\"" +
    "}, {" +
    "    \"file\": [{" +
    "            \"fileRefNo\": \"AG/CSD/2\"," +
    "            \"status\": \"Active\"" +
    "        }]," +
    "    \"requestNo\": \"225SRV\"" +
    "}]");
JsonNode node = josson.getNode("group(requestNo, file).field(file.flatten(1))");
System.out.println(node.toPrettyString());

Output

[ {
  "requestNo" : "225V49",
  "file" : [ {
    "fileRefNo" : "AG/CSD/1",
    "status" : "Active"
  } ]
}, {
  "requestNo" : "225SRV",
  "file" : [ {
    "fileRefNo" : "AG/CSD/1",
    "status" : "Inactive"
  }, {
    "fileRefNo" : "AG/CSD/2",
    "status" : "Active"
  } ]
} ]
Raymond Choi
  • 1,065
  • 2
  • 7
  • 8