JOLT 变换将元素添加到数组 [英] JOLT transformation add element to array

查看:40
本文介绍了JOLT 变换将元素添加到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

输入

<代码>{选项": [{"name": "会",状态":启用"},{"name:": "伯特",状态":启用"},{"name": "凯特",状态":已禁用"}]}

震动规格

<预><代码>[{操作":默认",规格":{选项[]": {3":{"name": "鲍勃",状态":启用"}}}}]

期望输出

<代码>{选项": [{"name": "会",状态":启用"},{"name": "伯特",状态":启用"},{"name": "凯特",状态":已禁用"},{"name": "鲍勃",状态":启用"}]}

如果输入数组长度为3就可以了.如何获取数组长度并动态设置索引?

解决方案

有点假,但可能.

规格

<预><代码>[{//默认在新的事物优先"中操作":默认",规格":{温度":{"name": "鲍勃",状态":启用"}}},{//首先复制选项数组,//然后将值(与 Bob 映射)复制到选项"//这是一个数组,所以 Shift 会将它添加到末尾"操作": "移位",规格":{"选项": "选项",温度":选项"}}]

I want to use a jolt transformation to add an element to an array.

My approach is to use default to append to the last element in the array

Input

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

Jolt Spec

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

Desired Output

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

It works if the input array length is 3. How can I obtain the array length and set the index dynamically?

解决方案

A little hokey, but possible.

Spec

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

这篇关于JOLT 变换将元素添加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆