英文:
Adding 1 to an array index in JOLT transformation
问题
I'm rather new to JOLT transformations so I'm still having trouble with pretty basic stuff.
I have the following spec:
{
"operation": "shift",
"spec": {
"positions": "positions",
"line_items": {
"*": {
"@(sku)": "id&",
"@(quantity)": "quantity&"
}
}
}
}
That generates this output out of some test data:
{
"positions": 2,
"id0": "149",
"quantity0": 1,
"id1": "4270",
"quantity1": 1,
"id2": "1440",
"quantity2": 3
}
My problem is that I need the attribute index to start at 1 instead of 0 ("id0" and "quantity0" should be "id1" and "quantity1" and so on)
I've been stuck trying to figure this one out
Any suggestions?
I tried googling this, play around with the spec myself, and even chatGPT... still can't seem to be any closer to an answer.
英文:
I'm rather new to JOLT transformations so I'm still having trouble with pretty basic stuff.
I have the following spec:
{
"operation": "shift",
"spec": {
"positions": "positions",
"line_items": {
"*": {
"@(sku)": "id&",
"@(quantity)": "quantity&"
}
}
}
}
That generates this output out of some test data:
{
"positions" : 2,
"id0" : "149",
"quantity0" : 1,
"id1" : "4270",
"quantity1" : 1,
"id2" : "1440",
"quantity2" : 3,
}
My problem is that I need the attribute index to start at 1 instead of 0 ("id0" and "quantity0" should be "id1" and "quantity1" and so on)
I've been stuck trying to figure this one out
Any suggestions?
I tried googling this, play around with the spec myself, and even chatGPT... still can't seem to be any closer to an answer.
答案1
得分: 1
以下是已翻译的内容:
考虑以下 JSON 输入,基于当前规范和结果,您可以使用默认转换规范来添加额外的对象,这将生成一个额外的对象,并在排序后成为第一个对象,例如:
[
{
"operation": "default",
"spec": {
"line_items": [{
"sku": "000000",
"quantity": "000000"
}]
}
},
{ // 为了准备将最新对象作为新数组中的第一个对象
"operation": "sort"
},
{
"operation": "shift",
"spec": {
"positions": "&",
"line_it*": {
"*": {
"sku": "id",
"*": "&"
}
}
}
},
{
"operation": "shift",
"spec": {
"positions": "&",
"id|qua*": {
"0": { "": "" }, // 摆脱最近生成的对象
"*": {
"@": "&2&" // 复制值并使用前缀键,前缀键是通过向上移动树的两层来重新构建的
}
}
}
}
]
网站http://jolt-demo.appspot.com/上的演示如下:
英文:
Considering
{
"positions": 2,
"line_items": [
{
"sku": "149",
"quantity": 1
},
{
"sku": "4270",
"quantity": 1
},
{
"sku": "1440",
"quantity": 3
}
]
}
is your input based on the current spec and result, you can add an extra object by using default transformation spec which will generate an extra object to be the first one after sorted such as
[
{
"operation": "default",
"spec": {
"line_ite": [{
"sku": "000000",
"quantity": "000000"
}]
}
},
{ // to prepare the latest object the be the first one within the new array
"operation": "sort"
},
{
"operation": "shift",
"spec": {
"positions": "&",
"line_it*": {
"*": {
"sku": "id",
"*": "&"
}
}
}
},
{
"operation": "shift",
"spec": {
"positions": "&",
"id|qua*": {
"0": { "": "" }, // get rid of the lately generated object
"*": {
"@": "&2&" // replicate the value with prefixed key which is reformed by going 2 levels up the tree
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论