JOLT转换将元素添加到数组

问题描述 投票:0回答:1

我想使用jolt转换将元素添加到数组中。

我的方法是使用default追加到数组中的最后一个元素

输入

{
  "options": [
    {
      "name": "Will",
      "state": "enabled"
    },
    {
      "name:": "Bert",
      "state": "enabled"
    },
    {
      "name": "Kate",
      "state": "disabled"
    }
  ]
}

Jolt Spec

[
  {
    "operation": "default",
    "spec": {
      "options[]": {
        "3": {
          "name": "Bob",
          "state": "enabled"
        }
      }
    }
  }
]

期望的输出

{
  "options": [
    {
      "name": "Will",
      "state": "enabled"
    },
    {
      "name": "Bert",
      "state": "enabled"
    },
    {
      "name": "Kate",
      "state": "disabled"
    },
    {
      "name": "Bob",
      "state": "enabled"
    }
  ]
}

如果输入数组长度为3,它可以工作。如何获取数组长度并动态设置索引?

json transformation jolt
1个回答
1
投票

一点点,但可能。

规格

[
  {
    // default in the new "thing first"
    "operation": "default",
    "spec": {
      "temp": {
        "name": "Bob",
        "state": "enabled"
      }
    }
  },
  {
    // copy the options array across first, 
    //  then copy the value (map with Bob) to "options"
    //  which is an array, so Shift will add it to the end
    "operation": "shift",
    "spec": {
      "options": "options",
      "temp": "options"
    }
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.