我已经努力将输入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"
}
]
}
答案 0 :(得分:2)
这不仅仅是“将数组转换为另一个数组”。
这里实际上有四件事情发生了:
这个规范是有效的,但考虑到发生了多少可能是脆弱的,而且奇怪的输入。
要了解这是做什么的,我建议打开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[]"
}
}
]