在JOLT转换中将1添加到数组索引中

huangapple go评论56阅读模式
英文:

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 在JOLT转换中将1添加到数组索引中

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 在JOLT转换中将1添加到数组索引中

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/上的演示如下:

在JOLT转换中将1添加到数组索引中

英文:

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

在JOLT转换中将1添加到数组索引中

huangapple
  • 本文由 发表于 2023年4月13日 16:07:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定