繁体   English   中英

使用 jolt 将数组转换为子值

[英]Transform array to sub-values using jolt

我正在努力使用 jolt 将我的输入响应转换为特定的 output,下面是我的示例

描述我的消息的输入

{
  "firstAttribute": true,
  "secondAttribute": "12",
  "data": {
    "propertyKey1": "1",
    "propertyKey2": [
      "a"
    ],
    "propertyKey3": "2",
    "propertyKey4": "3",
    "propertyKey_test": [
      "option1",
      "option2",
      "option3"
    ],
    "propertyKey5": "4",
    "propertyKey6": "87.0"
  },
  "Keytest1": "value1",
  "KeyTest2": "value2"
}  

使用的颠簸规格

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "propertyKey2": {
          "0": "propertyKey2"
        },
        "*": {
          "@": "data.&"
        }
      },
      "*": {
        "@": "&"
      }
    }
  }
]

颠簸转换后我的实际 output


{
  "firstAttribute" : true,
  "secondAttribute" : "12",
  "data" : {
    "propertyKey1" : "1",
    "propertyKey3" : "2",
    "propertyKey4" : "3",
    "propertyKey_test" : [ "option1", "option2", "option3" ],
    "propertyKey5" : "4",
    "propertyKey6" : "87.0"
  },
  "propertyKey2" : "a",
  "Keytest1" : "value1",
  "KeyTest2" : "value2"
}

所需的 output 是将每个数组字段转换为单独的字段,例如propertyKey_test ,如下所示


{
  "firstAttribute" : true,
  "secondAttribute" : "12",
  "data" : {
    "propertyKey1" : "1",
    "propertyKey3" : "2",
    "propertyKey4" : "3",
    "propertyKey_test": "option1",
    "propertyKey5" : "4",
    "propertyKey6" : "87.0"
  },
  "propertyKey2" : "a",
  "Keytest1" : "value1",
  "KeyTest2" : "value2"
}

任何帮助,将不胜感激。 提前致谢

PS:我们可以接收动态数组字段(例如:今天我们有 propertyKey_test:["option1","option2","option3"] 明天我们可以接收另一个数组字段,例如 new_field:["a","b", “c”]。我的目标是每次检查该字段是否为数组,并如上所述仅获取第一个元素

当前所需的 output 不是有效的 JSON 值,因为在一个公共 object 中可能不存在多个具有相同键的属性。但是,如果您需要区分"propertyKey_test"数组的组件,则使用如下所示的数组可能是首选,例如

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "propertyKey2": {
          "0": "&1"
        },
        "*": {
          "@": "&2.&"
        },
        "propertyKey_*": {
          "*": {
            "@": "&3.&2&1"
          }
        }
      },
      "*": {
        "@": "&"
      }
    }
  }
]

为了得到

{
  "firstAttribute" : true,
  "secondAttribute" : "12",
  "data" : {
    "propertyKey1" : "1",
    "propertyKey3" : "2",
    "propertyKey4" : "3",
    "propertyKey_test0" : "option1",
    "propertyKey_test1" : "option2",
    "propertyKey_test2" : "option3",
    "propertyKey5" : "4",
    "propertyKey6" : "87.0"
  },
  "propertyKey2" : "a",
  "Keytest1" : "value1",
  "KeyTest2" : "value2"
}

如 output。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM