【发布时间】:2021-10-22 15:50:41
【问题描述】:
我有这个输入
[
{
"attributeKey": "foo",
"stringValue": "fooValue",
},
{
"attributeKey": "bar",
"stringValue": "barValue1¤barValue2",
}
]
我想把它转换成这个输出
{
"foo" : "fooValue",
"bar" : ["barValue1", "barValue2"]
}
规则是:
- 每当我们在输入数组中发现一个元素的属性
stringValue包含 ¤ 字符时,我们都应该将其拆分/转换为字符串数组。 - 输出中值结果的类型非常重要:对于
attributeKey具有包含 ¤ 分隔符的stringValue,结果应该是字符串数组(拆分值),否则结果应该是一个字符串。
请不要事先不知道 attributeKey 的值,它们可能随时更改!
我已经尝试过这个规范:
[
{
"operation": "shift",
"spec": {
"*": {
"@(1,[&].stringValue)": {
"*": {
"$": "@(3,attributeKey)"
},
"*\\¤*": {
"$": "@(3,attributeKey)"
}
}
}
}
}
]
但它只给了我这个:
{
"foo" : "fooValue",
"bar" : "barValue1¤barValue2"
}
我多次尝试拆分结果但没有成功!
更新 @barbaros 解决方案,添加:
{
"operation": "modify-overwrite-beta",
"spec": {
"bar": "=split('¤', @(1,&))"
}
}
只有在我们知道attributeKey 的值的情况下才会给出所需的结果,例如在我的情况下,我可以收到一个包含不同值的 json: p>
[
{
"attributeKey": "baz",
"stringValue": "bazValue1¤bazValue2",
},
]
在这种情况下,提供的解决方案将不起作用,因为 attributeKey 的值每次都会改变!因此,无论输入中attributeKey 的值如何,我们都应该能够执行拆分/转换。
【问题讨论】: