JOLT转换将元素添加到数组

时间:2017-12-18 22:31:56

标签: json transformation jolt

我想使用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,它是否有效。如何获取数组长度并动态设置索引?

1 个答案:

答案 0 :(得分: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"
    }
  }
]
相关问题