我已经尽力将输入json字符串转换为另一个使用Jolt,如下所示。但我无法编写正确的Jolt Spec来实现转换,Jolt可以做这样的事吗?谢谢!
输入:
{
"result": [
{
"name": "AAA",
"value1": "AAA-111",
"value2": "AAA-222",
"value3": "AAA-333"
},
{
"name": "BBB",
"value1": "BBB-111",
"value2": "BBB-222",
"value3": "BBB-333"
},
{
"name": "CCC",
"value1": "CCC-111",
"value2": "CCC-222",
"value3": "CCC-333"
}
]
}
输出:
{
"result": [
{
"value_name":"value1",
"AAA":"AAA-111",
"BBB":"BBB-111",
"CCC":"CCC-111"
},
{
"value_name":"value2",
"AAA":"AAA-222",
"BBB":"BBB-222",
"CCC":"CCC-222"
},
{
"value_name":"value3",
"AAA":"AAA-333",
"BBB":"BBB-333",
"CCC":"CCC-333"
}
]
}
这不仅仅是“将数组转换为另一个数组”。
这里实际上有四件事:
这个规范是有效的,但考虑到正在发生的事情可能是脆弱的,而且奇怪的输入。
为了理解这是做什么,我建议打开jolt-demo站点的4个浏览器选项卡,单独运行每个移位操作,将一个选项卡的输出复制到下一个选项卡的输入中。
这实际上就是我写这篇文章的方式。
规格
[
{
"operation": "shift",
"spec": {
"result": {
"*": {
// first group all the data by valueKEY
"value*": "&[]"
}
}
}
},
{
// next build the 'AAA':'AAA-111' structure
// but not yet in the final result array
"operation": "shift",
"spec": {
"value*": { // should be value1, value2, etc
"*": { // array index
"*-*": {
// keep top level value1, value2
// but now build the 'AAA':'AAA-111' logic
// This is assuming that it is hypen "-" delimited
"@1": "&3.&(1,1)"
}
}
}
}
},
{
// now build 'value_name':'value1'
"operation": "shift",
"spec": {
"value*": {
// pass thru the "AAA':'AAA-111' data
"*": "&1.&",
//
// push value name down to be siblings of 'AAA':'AAA-111'
"$": "&1.value_name"
}
}
},
{
// finally now that the core data is all setup, accumulate
// into an array
"operation": "shift",
"spec": {
"value*": "result[]"
}
}
]