【问题标题】:How to rename all the elements in array in json using jolt?如何使用 jolt 重命名 json 中数组中的所有元素?
【发布时间】:2019-10-23 17:06:49
【问题描述】:

我有来自外部应用程序 A 的 JSON 数据需要发送到应用程序 B 。并且在数据进入应用B之前,需要对json中的所有属性进行重命名。

比如传入的json数据结构是-

{
  "Array1": [
    {
      "field1": "foo1",
      "field2": [
        "bar1",
        "bar2"
      ]
    },
    {
      "field1": "foo2",
      "field2": [
        "bar3",
        "bar4"
      ],

      ...

        {
      "field1": "fooN",
      "field2": [
        "barX",
        "barY"
      ] 

    }
  ]
}

Array1 中的元素数量在每条记录中是可变的。

预期的输出是 -

{
  "ElementList": [
    {
      "Attr1": "foo1",
      "Attr2": [
        "bar1",
        "bar2"
      ]
    },
    {
      "Attr1": "foo2",
      "Attr2": [
        "bar3",
        "bar4"
      ],

      ...

        {
      "Attr1": "fooN",
      "Attr2": [
        "barX",
        "barY"
      ]  

    }
  ]
}

基本上

  • Array1 重命名为 ElementList
  • Array1 中的每个 Field1 都重命名为 Attr1
  • 每个 Field2 Array1 重命名为 Attr2

对于简单的重命名,我可以使用shift 运算符,但我无法为数组指定正确的颠簸转换。有任何想法吗?

【问题讨论】:

标签: javascript json jolt


【解决方案1】:

看看这是否适合你:

[
  {
    "operation": "shift",
    "spec": {
      "Array1": "ElementList"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "ElementList": {
        "*": {
          "field1": "ElementList[&1].Attr1",
          "field2": "ElementList[&1].Attr2"
        }
      }
    }
  }
]

【讨论】:

  • 谢谢!是的,这就是我根据其他用户指出的相关问题提出的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-17
  • 2022-11-05
  • 2019-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多