2

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

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

例子:

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 并遵循我们自己的约定,但想检查是否有任何标准格式/开源工具来指定或简化从源到目标的字段映射?

4

1 回答 1

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" }
于 2019-04-24T21:22:54.650 回答