【问题标题】:JOLT transformation add element to arrayJOLT 转换将元素添加到数组
【发布时间】:2017-12-18 22:31:55
【问题描述】:

我想使用颠簸变换将元素添加到数组中。

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

输入

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

震动规格

[
  {
    "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】:

    有点做作,但可能。

    规格

    [
      {
        // 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"
        }
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多