【问题标题】:JOLT spec for permutaionJOLT 置换规范
【发布时间】:2016-03-25 08:05:02
【问题描述】:

您能帮我为以下内容编写 JOLT 规范吗?

输入:

{
    "title": [
        "vsnu",
        "anothervsnu"
    ],
    "anothertitle": [
        "vsnu",
        "anothervsnu"
    ]
}

预期输出:

{
Response : [
{
"head" : "title",
"name" : "vsnu"
},
{
"head" : "title",
"name" : "anothervsnu"
},
{
"head" : "anothertitle",
"name" : "vsnu"
},
{
"head" : "anothertitle",
"name" : "anothervsnu"
}
]
}

过去 3 天我都陷入了困境。请帮助我。 我希望上面的问题能解释期望,我写这个只是因为 StackOverflow 显示了验证错误消息。

提前致谢。

【问题讨论】:

    标签: javascript java json transformation jolt


    【解决方案1】:

    您只需要两个迭代器,一个在输入的键上,一个在属性的数组上。

    function buildObject(o) {
        var result = [];
    
        Object.keys(o).forEach(function (k) {
            o[k].forEach(function (a) {
                result.push({ head: k, name: a });
            });
        });
        return { Response: result };
    }
    
    var input = { "title": ["vsnu", "anothervsnu"], "anothertitle": ["vsnu", "anothervsnu"] },
        output = buildObject(input);
    
    document.write('<pre>' + JSON.stringify(output, 0, 4) + '</pre>');

    【讨论】:

      【解决方案2】:

      因为 Jolt “shift” 一次处理一个项目,所以它无法写入映射数组的输出。

      可以,但需要两班倒。第一个构建“head”和“name”的并行数组,第二个使用并行数组中的索引号将它们旋转到 Response 数组中。

      规格

      [
        {
          "operation": "shift",
          "spec": {
            "*": { // title or anothertitle
              "*": { // array index
                "*": { // actual array value "vsnu"
                  "$2": "head[]", // for each array value grab a copy of the "title"
                  "$": "name[]"
                }
              }
            }
          }
        },
        {
          "operation": "shift",
          "spec": {
            "head": {
              "*": "Response[&].head"
            },
            "name": {
              "*": "Response[&].name"
            }
          }
        }
      ]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-27
        • 1970-01-01
        • 1970-01-01
        • 2023-02-12
        • 1970-01-01
        • 2023-04-11
        • 1970-01-01
        相关资源
        最近更新 更多