英文:
How can I insert an array into a specific array element using Jolt
问题
以下是您要翻译的内容:
Question/problem
如何使用Jolt将数组插入到数组中的特定对象?
input
这些对象具有一组共同的属性,但支持可选属性。
特定对象需要根据它们在数组中的特定位置,即第二个([1]
),获取这些可选属性。这些可选属性将作为jolt转换的一部分添加。
{
"array" : [
{
"key" : "a key",
"value" : "a value"
},
{
"key" : "another key",
"value" : "another value"
}
]
}
expected
{
"array" : [
{
"key" : "a key",
"value" : "a value"
},
{
"key" : "another key",
"value" : "another value",
"values":["extra value1", "extra value2"]
}
]
}
What has been tried
我尝试过default
,modify-default-beta
和modify-overwrite-beta
,但似乎都不按我想象的方式工作。
default
似乎默认情况下会忽略数组引用"[1]"
,所以我尝试了似乎支持此操作的beta操作。
default spec
[
{
"operation": "default",
"spec": {
"array": {
"[1]": {
"values": ["extra value1", "extra value2"]
}
}
}
}
]
default actual
{
"array" : [ {
"key" : "a key",
"value" : "a value"
}, {
"key" : "another key",
"value" : "another value"
} ]
}
modify-default-beta
解析了数组引用"[1]"
,但仅将规范中的数组的第一个元素应用于数组的整个部分。
modify-default-beta spec
[
{
"operation": "modify-default-beta",
"spec": {
"array": {
"[1]": {
"values": ["extra value1", "extra value2"]
}
}
}
}
]
modify-default-beta actual
{
"array" : [ {
"key" : "a key",
"value" : "a value"
}, {
"key" : "another key",
"value" : "another value",
"values" : "extra value1" // 数组被丢弃
} ]
}
modify-overwrite-beta
尝试modify-overwrite-beta
,结果与modify-default-beta
相同。
modify-overwrite-beta spec
[
{
"operation": "modify-overwrite-beta",
"spec": {
"array": {
"[1]": {
"values": ["extra value1", "extra value2"]
}
}
}
}
]
modify-overwrite-beta actual
{
"array" : [ {
"key" : "a key",
"value" : "a value"
}, {
"key" : "another key",
"value" : "another value",
"values" : "extra value1" // 数组被丢弃
} ]
}
Interesting side note
没有输入中的数组引用,这是一个相当简单的问题,似乎当存在数组引用和*-beta
操作时,行为不是我期望的。
simple default
我从输入中去掉了数组
simple default input
{
"element1": {
"key": "a key",
"value": "a value"
},
"element2": {
"key": "a key",
"value": "another value"
}
}
simple default spec
[
{
"operation": "default",
"spec": {
"element2": {
"values": ["extra value1", "extra value2"]
}
}
}
]
simple default actual
{
"element1" : {
"key" : "a key",
"value" : "a value"
},
"element2" : {
"key" : "another key",
"value" : "another value",
"values" : [ "extra value1", "extra value2" ]
}
}
<details>
<summary>英文:</summary>
# Question/problem
How can I use [Jolt][1] to insert an array into a specific object in an array?
# input
The objects have a common set of attributes, but support optional attributes.
Specific objects need to get these optional attributes depending on their specific position in the array, i.e. second (`[1]`). These optional attributes are to be added as part of the jolt transformation.
```json
{
"array" : [
{
"key" : "a key",
"value" : "a value"
},
{
"key" : "another key",
"value" : "another value"
}
]
}
expected
{
"array" : [
{
"key" : "a key",
"value" : "a value"
},
{
"key" : "another key",
"value" : "another value",
"values": ["extra value1", "extra value2"]
}
]
}
What has been tried
I've tried default
, modify-default-beta
, and modify-overwrite-beta
and none seem to behave the way I thought they did.
default
Seems like default ignores the array reference "[1]"
so tried the beta operations which seem to support this.
default spec
[
{
"operation": "default",
"spec": {
"array": {
"[1]": {
"values": ["extra value1", "extra value2"]
}
}
}
}
]
default actual
{
"array" : [ {
"key" : "a key",
"value" : "a value"
}, {
"key" : "another key",
"value" : "another value"
} ]
}
modify-default-beta
Resolves the array reference "[1]"
, but only applies the first element in the array from the spec and not the whole array
modify-default-beta spec
[
{
"operation": "modify-default-beta",
"spec": {
"array": {
"[1]": {
"values": ["extra value1", "extra value2"]
}
}
}
}
]
modify-default-beta actual
{
"array" : [ {
"key" : "a key",
"value" : "a value"
}, {
"key" : "another key",
"value" : "another value",
"values" : "extra value1" // array is dumped
} ]
}
modify-overwrite-beta
Tried modify-overwrite-beta
and I get the same behaviour as modify-default-beta
.
modify-overwrite-beta spec
[
{
"operation": "modify-overwrite-beta",
"spec": {
"array": {
"[1]": {
"values": ["extra value1", "extra value2"]
}
}
}
}
]
modify-overwrite-beta actual
{
"array" : [ {
"key" : "a key",
"value" : "a value"
}, {
"key" : "another key",
"value" : "another value",
"values" : "extra value1" // array is dumped
} ]
}
Interesting side note
Without the array reference on the input this is a fairly easy problem, it seems the behaviour is not what I am expecting when there is an array reference and the *-beta
operations.
simple default
I've gotten rid of the array from the input
simple default input
{
"element1": {
"key": "a key",
"value": "a value"
},
"element2": {
"key": "a key",
"value": "another value"
}
}
simple default spec
[
{
"operation": "default",
"spec": {
"element2": {
"values": ["extra value1", "extra value2"]
}
}
}
]
simple default actual
{
"element1" : {
"key" : "a key",
"value" : "a value"
},
"element2" : {
"key" : "another key",
"value" : "another value",
"values" : [ "extra value1", "extra value2" ]
}
}
答案1
得分: 0
这似乎是我对规范用法的误解。
看起来你不是插入一个数组,而是要插入每个要插入的元素。
上述规范将两个值插入到值属性中,具体为元素"0"
和元素"1"
。
这也适用于modify-overwrite-beta
操作。
这两个规范的结果如下:
{
"array": [{
"key": "a key",
"value": "a value"
}, {
"key": "another key",
"value": "another value",
"values": ["extra value1", "extra value2"]
}]
}
英文:
This appears to be my misunderstanding of the usage of the spec.
It appears that you don't insert an array, but each element that you want to insert.
[
{
"operation": "modify-default-beta",
"spec": {
"array": {
"[1]": {
"values": {
"[0]": "extra value1",
"[1]": "extra value2"
}
}
}
}
}
]
The spec above is inserting both values into the values attribute and specifically into element "[0]"
and element "[1]"
.
This also works with the modify-overwrite-beta
operation
[
{
"operation": "modify-overwrite-beta",
"spec": {
"array": {
"[1]": {
"values": {
"[0]": "extra value1",
"[1]": "extra value2"
}
}
}
}
}
]
the result of both of these specs is as follows:
{
"array" : [ {
"key" : "a key",
"value" : "a value"
}, {
"key" : "another key",
"value" : "another value",
"values" : [ "extra value1", "extra value2" ]
} ]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论