指定字段映射的标准格式

Standard format for specifying the mapping of fields

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

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

示例:

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

假设您正在尝试转换:

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

至:

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

您可以使用任何执行 JSON 转换的库,例如 this or 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" }

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