用于指定字段映射的标准格式

时间:2019-04-23 05:59:33

标签: interface architecture mapping standards system-integration

我们正在尝试构建一个可以与多个系统集成的界面。

因此,我们需要一种方法来指定不同系统之间标准字段的映射。

示例:

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)

我们可以简单地创建此类映射的JSON并遵循我们自己的约定,但想检查是否存在用于指定或简化字段从源到目标的映射的标准格式/开源工具?

1 个答案:

答案 0 :(得分:1)

假设您要转换:

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

收件人:

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

您可以使用任何进行JSON转换的库,例如thisthis。例如,您可以这样定义转换:

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" }

将Java与Jolt一起使用(我无法对其进行测试,但这是通常的想法):

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" }