Jolt – 根据输入数组拆分对象

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

Jolt - Split objects on the basis of input array

问题

在Jolt中,我想根据地址数组中的项来修改我的对象。对象应该根据数组的项分成多个对象。请协助解决这个问题。谢谢。

预期输出:

[
  {
    "name": "Mike",
    "dept": "IT",
    "address": "Austria"
  },
  {
    "name": "Mike",
    "dept": "IT",
    "address": "India"
  },
  {
    "name": "Mike",
    "dept": "IT",
    "address": "US"
  },
  {
    "name": "Shelly",
    "dept": "Pharma",
    "address": "Finland"
  },
  {
    "name": "Shelly",
    "dept": "Pharma",
    "address": "UK"
  }
]
英文:

In Jolt, I want to modify my objects on the basis of items inside the address array. The object should split into the multiple objects on the basis of items of the array. Please assist in solving this. Thankyou.

Input:

[
  {
    "name": "Mike",
    "dept": "IT",
    "address": [
      "Austria",
      "India",
      "US"
    ]
  },
  {
    "name": "Shelly",
    "dept": "Pharma",
    "address": [
      "Finland",
      "UK"
    ]
  }
]

Expected output:

[
  {
    "name": "Mike",
    "dept": "IT",
    "address": "Austria"
  },
  {
    "name": "Mike",
    "dept": "IT",
    "address": "India"
  },
  {
    "name": "Mike",
    "dept": "IT",
    "address": "US"
  },
  {
    "name": "Shelly",
    "dept": "Pharma",
    "address": "Finland"
  },
  {
    "name": "Shelly",
    "dept": "Pharma",
    "address": "UK"
  }
]

答案1

得分: 2

你可以使用以下的 shift 转换规范,其中返回的属性基于两个分组进行平铺:

  1. 主数组内对象的索引,
  2. address 数组的索引

例如:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "address": {
          "*": {
            "@2,name": "&3.&1.name", // 向上遍历 2 级树来获取相关值
            "@2,dept": "&3.&1.dept",
            "@": "&3.&1.&2"
          }
        }
      }
    }
  },
  { // 去除对象键
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

在网站 http://jolt-demo.appspot.com/ 上的 演示 如下图所示:

Jolt – 根据输入数组拆分对象

英文:

You can use the following shift transformation spec in which the returned attributes are tiled based on the two groupings :

  1. the indexes of the objects within the main array,
  2. the indexes of the address array

such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "address": {
          "*": {
            "@2,name": "&3.&1.name", // go 2 levels up the tree to grab the related value
            "@2,dept": "&3.&1.dept",
            "@": "&3.&1.&2"
          }
        }
      }
    }
  },
  { // get rid of object keys
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is

Jolt – 根据输入数组拆分对象

huangapple
  • 本文由 发表于 2023年7月11日 13:42:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658960.html
匿名

发表评论

匿名网友

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

确定