JAVA JSON dynamic conversion from nested JSON to flat JSON

vikram :

In java, I am trying to convert nested JSON to flat JSON .Below is the input payload.

INPUT PAYLOAD:

{
  "root": {
    "data": {
      "a": "1",
      "b": {
        "c": "2"
      },
      "test": [
        {
          "e": "3"
        },
        {
          "f": "4"
        }
      ],
      "test1": [
        {
          "g": "5"
        }
      ],
      "test2": [
        1,
        2,
        3
      ]
    }
  }
}

From the above payload I am trying to convert to the following flattened JSON.

{
 "root.data.a":"1",
 "root.data.b.c":"2",
 "root.data.test[]":[
        {
          "e": "3"
        },
        {
          "f": "4"
        }
      ],
  "root.data.test1[].g":"5",
  "root.data.test2":[1,2,3]
}

In the above JSON following conditions are used while converting to flat JSON.

  1. If JSONObject consists of nested JSONObject and value as any datatype except JSONArray then key and value of flat JSON must be "root.data.a" and value as 1.

  2. If JSONObject consists of JSONArray and the length of the JSONArray is greater than 1 than flat JSON must key must be "root.data.test[]" and value as full JSONArray.

  3. If JSONObject consists of JSONArray and the length of the JSONArray is 1 than flat JSON must key must consists of all the JSON object elements in JSON array, "root.data.test1[].g" and value is "5".

  4. If the JSON key has value as Array which is not having any JSON Object then assign value to that path in flat JSON like the key as "root.data.test2" and value as [1,2,3]

So, how can Convert to flat JSON from input JSON .

Himanshi :
public class Sample {
    public static void main(String[] args) {
        Sample a= new Sample();
        a.testCreatingKeyValues();
    }

    String json = 
        "{\n  \"root\": {\n    \"data\": {\n      \"a\": \"1\",\n      \"b\": {\n        \"c\": \"2\"\n      },\n      \"test\": [\n        {\n          \"e\": \"3\"\n        },\n        {\n          \"f\": \"4\"\n        }\n      ],\n      \"test1\": [\n        {\n          \"g\": \"5\"\n        }\n      ],\n      \"test2\": [\n        1,\n        2,\n        3\n      ]\n    }\n  }\n}";

        //  @Test
          public void testCreatingKeyValues() {
            Map<String, Object> map = new HashMap<String, Object>();
            try {
              addKeys("", new ObjectMapper().readTree(json), map);
            } catch (IOException e) {
              e.printStackTrace();
            }
            System.out.println(map);
          }

          private void addKeys(String currentPath, JsonNode jsonNode, Map<String, Object> map) {
            if (jsonNode.isObject()) {
              ObjectNode objectNode = (ObjectNode) jsonNode;
              Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields();
              String pathPrefix = currentPath.isEmpty() ? "" : currentPath + ".";

              while (iter.hasNext()) {
                Map.Entry<String, JsonNode> entry = iter.next();
                addKeys(pathPrefix + entry.getKey(), entry.getValue(), map);
              }
            } else if (jsonNode.isArray()) {
              ArrayNode arrayNode = (ArrayNode) jsonNode;
              map.put(currentPath+"[]", arrayNode);

            } else if (jsonNode.isValueNode()) {
              ValueNode valueNode = (ValueNode) jsonNode;
              map.put(currentPath, valueNode.asText());
            }
          }
}

giving the output:

{root.data.b.c=2, root.data.test2[]=[1,2,3], root.data.test1[]=[{"g":"5"}], root.data.a=1, root.data.test[]=[{"e":"3"},{"f":"4"}]}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=396510&siteId=1