0

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

输入:

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

预期输出:

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

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

提前致谢。

4

2 回答 2

0

因为 Jolt “shift” 一次处理一个项目,所以它在写入一个 Map 数组的输出时遇到了麻烦。

可以做到,但需要两班倒。第一个构建“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"
      }
    }
  }
]
于 2016-03-26T06:11:08.243 回答
0

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

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>');

于 2016-03-25T08:25:56.607 回答