Search code examples
interfacearchitecturemappingstandardssystem-integration

Standard format for specifying the mapping of fields


We are trying to build a single interface which can be integrated with multiple systems.

Hence we need a way to specify the mappings of standard fields across different systems.

Example:

Interface1 has the following fields: A, B, C

Interface2 has the following fields: P, Q, R

We need to map fields: A -> Q, B -> P and C -> R (just an example)

We can simply create a JSON of such mappings and follow our own convention, but wanted to check if there are any standard formats / open source tools for specifying or simplifying the mapping of fields from source to destination?


Solution

  • Assuming you are trying to convert:

    Response from interface 1  = { "A": "value1", "B": "value2", "C": "value3" }
    

    to:

    Response from interface 2  = { "Q": "value1", "P": "value2", "R": "value3" }
    

    You can use any library that does JSON transformations such as this or this. For instance, you could define your transformation like this:

    var template = {
        Q: '$.A',
        P: '$.B',
        R: '$.C'
    };
    
    var input = { "A": "value1", "B": "value2", "C": "value3" };
    var output = jsonPathObjectTransform(input, template);
    
    // -> output = { "Q": "value1", "P": "value2", "R": "value3" }
    

    Using Java with Jolt (I was unable to test it, but that's the general idea):

    String input = "{\n" +
        "  \"A\": \"value1\",\n" +
        "  \"B\": \"value2\",\n" +
        "  \"C\": \"value3\"\n" +
        "}"
    
    String spec = "[\n" +
        "  {\n" +
        "    \"operation\": \"shift\",\n" +
        "    \"spec\": {\n" +
        "      \"A\": \"Q\",\n" +
        "      \"B\": \"P\",\n" +
        "      \"C\": \"R\"\n" +
        "    }\n" +
        "  }\n" +
        "]\n"
    
    Chainr chainr = JsonUtils.jsonToList(spec);
    Object output = chainr.transform(input);
    
    // -> output = { "Q": "value1", "P": "value2", "R": "value3" }