MK dev
MK dev

Reputation: 67

How to map one JSON schema to another?

I have a requirement where I need to map an input JSON file to a specific output JSON schema, but not sure how to proceed with this. Should I proceed with converting from JSON to Java object, transform and convert back to JSON or is there any other way to handle this?

My Input and output JSON looks like the examples below.

Input:

[{
  "FirstName": "fgfgf",
  "BIRTH_DATE": "1988-06-25",
  "frm": "sdsd67ds-4937-fdf",
  "GENDER_CODE": "M",
  "ID": "4083-9b78-eab99231adc9",
  "LastName": "ddgd"
}, {
  "FirstName": "gfgf",
  "BIRTH_DATE": "1973-03-02",
  "frm": "7e3b3dd6-97bc-d918065a642f",
  "GENDER_CODE": "O",
  "ID": "88e9-0253a00cde91",
  "LastName": "gfgsfg"
}, {
  "FirstName": " dfsdfsdfgsd",
  "BIRTH_DATE": "1950-02-26",
  "frm": "9d30-9f23ed77ea1a",
  "GENDER_CODE": "M",
  "ID": "9b1b-090c179a31ea",
  "LastName": "RU dddas"
}, {
  "cStage df": "cStage df",
  "dds": "cM1y",
  "ddsc": "cTgb",
  "grgfd": "cK1",
  "BId": "a431a5b4-4176-704bed5021f8",
  "fsfsdfsd": "7face4c8-ffa870cfbf2d"
}, {
  "fvf": "cStage dgd",
  "dsdf": "cMff",
  "fgsd": "cTgf",
  "FirstName": "dfdfsdsf",
  "dfs": "c1",
  "BId": "a43-9d6d-704bed5021f8",
  "BIRTH_DATE": "1999-08-30",
  "msdrsdn": "727146-8e930fc1f662",
  "GENDER_CODE": "F",
  "ID": "6560a6cb-e01757819bcc",
  "LastName": "dfsdfsd"
}]

Output:

[{
    "SENDER_NAME": "XYZ",
    "MARITAL_STATUS": "Unmarried",
    "GENDER_CODE": "F",
    "PHONE_NO": "576756754",
    "LAST_NAME": "ghghg",
    "FIRST_NAME": "hgdghd",
    "PHONE_CODE": "91",
    "GENDER": "F",
    "ID": "5765474",
    "ADDRESS": "500 fgdfgd,gdf",
    "BIRTH_DATE": 1523877361463,
    "gdfgg": "-86afad953b34",
    "DATE_TIME": 56546456,
    "A_BIRTH_DATE": "567546456",
    "C_INFO": {
      "CLINICAL": {
        "T": "1",
        "N": "0",
        "M": "1",
        "STAGE": "Stage I"
      },
      "PATHOLOGY": {
        "T": "1",
        "N": "1",
        "M": "1",
        "STAGE": "Stage I"
      }
    }
  },
  {
    "SENDER_NAME": "ABC",
    "MARITAL_STATUS": "Unmarried",
    "GENDER_CODE": "M",
    "PHONE_NO": "56456456",
    "LAST_NAME": "hghfg",
    "FIRST_NAME": "dgdfgd",
    "PHONE_CODE": "91",
    "GENDER_CODE": "M",
    "ID": "6456456456",
    "FULL_ADDRESS": "fgdfdfghdf,fdgfdg,fgfd45345",
    "BIRTH_DATE": 1523877413779,
    "ID": "b84e-4866a656cce8",
    "DATE_TIME": 1523877413779,
    "BIRTH_DATE": "67567657",
    "C_INFO": {
      "CLINICAL": {
        "T": "0",
        "N": "0",
        "M": "0",
        "STAGE": "Stage 0"
      },
      "PATHOLOGY": {
        "T": "0",
        "N": "0",
        "M": "0",
        "STAGE": "Stage 0"
      }
    }
  },
  {
    "SENDER_NAME": "RAC",
    "MARITAL_STATUS": "Unmarried",
    "GENDER_CODE": "F",
    "PHONE_NO": "56546546",
    "LAST_NAME": "hfghf",
    "FIRST_NAME": "gfhfghfg",
    "PHONE_CODE": "91",
    "GENDER_CODE": "M",
    "ID": "676745674",
    "_ADDRESS": "fgdfdfg",
    "BIRTH_DATE": 6756756756,
    "ID": "2e-a0d8-hghfg67ggh",
    "DATE_TIME": "Mon March 20 17:52:04 IST 2018",
    "BIRTH_DATE": "05212014",
    "C_INFO": {
      "CLINICAL": {
        "T": "0",
        "N": "0",
        "M": "1",
        "STAGE": "Stage IA"
      },
      "PATHOLOGY": {
        "T": "1",
        "N": "0",
        "M": "1",
        "STAGE": "Stage IB"
      }
    }
  }
]

Upvotes: 2

Views: 7285

Answers (2)

wandermonk
wandermonk

Reputation: 7356

You can use the Jackson API to add field or transform a JSON without creating POJO's. It provides a object form called JsonNode ,JsonObject and JsonArray types which can be transformed like i did in the below code. I hope this helps you.

import java.io.IOException;
import java.util.Iterator;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class JsonTest {

    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws JsonProcessingException, IOException {
        String jsoninpt = "[{\r\n" + 
                "  \"FirstName\": \"fgfgf\",\r\n" + 
                "  \"BIRTH_DATE\": \"1988-06-25\",\r\n" + 
                "  \"frm\": \"sdsd67ds-4937-fdf\",\r\n" + 
                "  \"GENDER_CODE\": \"M\",\r\n" + 
                "  \"ID\": \"4083-9b78-eab99231adc9\",\r\n" + 
                "  \"LastName\": \"ddgd\"\r\n" + 
                "}, {\r\n" + 
                "  \"FirstName\": \"gfgf\",\r\n" + 
                "  \"BIRTH_DATE\": \"1973-03-02\",\r\n" + 
                "  \"frm\": \"7e3b3dd6-97bc-d918065a642f\",\r\n" + 
                "  \"GENDER_CODE\": \"O\",\r\n" + 
                "  \"ID\": \"88e9-0253a00cde91\",\r\n" + 
                "  \"LastName\": \"gfgsfg\"\r\n" + 
                "}, {\r\n" + 
                "  \"FirstName\": \" dfsdfsdfgsd\",\r\n" + 
                "  \"BIRTH_DATE\": \"1950-02-26\",\r\n" + 
                "  \"frm\": \"9d30-9f23ed77ea1a\",\r\n" + 
                "  \"GENDER_CODE\": \"M\",\r\n" + 
                "  \"ID\": \"9b1b-090c179a31ea\",\r\n" + 
                "  \"LastName\": \"RU dddas\"\r\n" + 
                "}, {\r\n" + 
                "  \"cStage df\": \"cStage df\",\r\n" + 
                "  \"dds\": \"cM1y\",\r\n" + 
                "  \"ddsc\": \"cTgb\",\r\n" + 
                "  \"grgfd\": \"cK1\",\r\n" + 
                "  \"BId\": \"a431a5b4-4176-704bed5021f8\",\r\n" + 
                "  \"fsfsdfsd\": \"7face4c8-ffa870cfbf2d\"\r\n" + 
                "}, {\r\n" + 
                "  \"fvf\": \"cStage dgd\",\r\n" + 
                "  \"dsdf\": \"cMff\",\r\n" + 
                "  \"fgsd\": \"cTgf\",\r\n" + 
                "  \"FirstName\": \"dfdfsdsf\",\r\n" + 
                "  \"dfs\": \"c1\",\r\n" + 
                "  \"BId\": \"a43-9d6d-704bed5021f8\",\r\n" + 
                "  \"BIRTH_DATE\": \"1999-08-30\",\r\n" + 
                "  \"msdrsdn\": \"727146-8e930fc1f662\",\r\n" + 
                "  \"GENDER_CODE\": \"F\",\r\n" + 
                "  \"ID\": \"6560a6cb-e01757819bcc\",\r\n" + 
                "  \"LastName\": \"dfsdfsd\"\r\n" + 
                "}]";

        String field = "{\r\n" + 
                "      \"CLINICAL\": {\r\n" + 
                "        \"T\": \"1\",\r\n" + 
                "        \"N\": \"0\",\r\n" + 
                "        \"M\": \"1\",\r\n" + 
                "        \"STAGE\": \"Stage I\"\r\n" + 
                "      },\r\n" + 
                "      \"PATHOLOGY\": {\r\n" + 
                "        \"T\": \"1\",\r\n" + 
                "        \"N\": \"1\",\r\n" + 
                "        \"M\": \"1\",\r\n" + 
                "        \"STAGE\": \"Stage I\"\r\n" + 
                "      }\r\n" + 
                "    }";


        //create JSON Node object from JSON String using ObjectMapper
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonFieldNode = mapper.readTree(field);
        JsonNode jsonInputNode = mapper.readTree(jsoninpt);

        System.out.println(mapper.writeValueAsString(jsonInputNode));
        //Iterate through the JsonNode elements inside the JSON Array
        Iterator<JsonNode> jsonInputNodeElements = jsonInputNode.elements();

        while(jsonInputNodeElements.hasNext()) {
            ObjectNode element = (ObjectNode) jsonInputNodeElements.next();
            //Add element to the JSON Object.
            element.put("c_info", mapper.writeValueAsString(jsonFieldNode));
        }

        System.out.println(mapper.writeValueAsString(jsonInputNode));

    }

}

Upvotes: 0

paultamalunas
paultamalunas

Reputation: 183

Jolt. This will allow you to do POJO-less transformations, as long as the formats aren't extremely different.

Upvotes: 3

Related Questions