【问题标题】:Jolt Convert string attribut to array only if it match to a condition仅当匹配条件时才将字符串属性转换为数组
【发布时间】:2021-10-22 15:50:41
【问题描述】:

我有这个输入

[
  {
    "attributeKey": "foo",
    "stringValue": "fooValue",
  },
  {
    "attributeKey": "bar",
    "stringValue": "barValue1¤barValue2",
  }
]

我想把它转换成这个输出

{ 
  "foo" : "fooValue",
  "bar" : ["barValue1", "barValue2"]
}

规则是:

  • 每当我们在输入数组中发现一个元素的属性 stringValue 包含 ¤ 字符时,我们都应该将其拆分/转换为字符串数组。
  • 输出中值结果的类型非常重要:对于 attributeKey 具有包含 ¤ 分隔符的 stringValue,结果应该是字符串数组(拆分值),否则结果应该是一个字符串。

请不要事先不知道 attributeKey 的值,它们可能随时更改!

我已经尝试过这个规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@(1,[&].stringValue)": {
          "*": {
            "$": "@(3,attributeKey)"
          },
          "*\\¤*": {
            "$": "@(3,attributeKey)"
          }
        }
      }
    }
  }
]

但它只给了我这个:

{
  "foo" : "fooValue",
  "bar" : "barValue1¤barValue2"
}

我多次尝试拆分结果但没有成功!


更新 @barbaros 解决方案,添加:

{
  "operation": "modify-overwrite-beta",
  "spec": {
    "bar": "=split('¤', @(1,&))"
  }
}

只有在我们知道attributeKey 的值的情况下才会给出所需的结果,例如在我的情况下,我可以收到一个包含不同值的 json: p>

[
  {
    "attributeKey": "baz",
    "stringValue": "bazValue1¤bazValue2",
  },
]

在这种情况下,提供的解决方案将不起作用,因为 attributeKey 的值每次都会改变!因此,无论输入中attributeKey 的值如何,我们都应该能够执行拆分/转换。

【问题讨论】:

    标签: arrays json jolt


    【解决方案1】:

    您可以将以下包含 split 功能的 modify-overwrite-beta 规范添加到当前 shift 规范

    {
      "operation": "modify-overwrite-beta",
      "spec": {
        "bar": "=split('¤', @(1,&))"
      }
    }
    

    编辑:

    根据您的需要,您可以制定 modify-overwrite-beta 规范。第一步,遍历数组的对象,然后将移位规范缩短如下

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "stringValue": "=split('¤', @(1,&))"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "@(2,[&].stringValue)": {
              "*": "@(2,attributeKey)"
            }
          }
        }
      }
    ]
    

    【讨论】:

    • attributeKey 的值可以随时更改,我没有固定值,而且如果stringValue 不包含¤(分隔符),则输出不应是数组,而是一个简单的字符串
    • 好吧,那些分隔符值@AbdelghaniRoussi 可以是什么?你能通过编辑问题来表达下一个解释(输出不应该是一个数组,而是一个简单的字符串)吗...?
    • 是的,我已经更新了问题
    猜你喜欢
    • 2014-09-28
    • 2012-06-18
    • 2016-09-19
    • 2021-10-31
    • 1970-01-01
    • 2018-01-30
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多