JOLT 转换过滤关于第一个字符串元素的列表

JOLT transformation filter a list about the first string element

我想在列表中搜索第一个真正的字符串元素(可能不是数字)并输出它。

输入 :

{
  "firstString": [
    "0.20",
    "test",
    "0.30"
  ]
}

{
  "firstString": [
    "0.20",
    "0,30",
    "test"
  ]
}

预期输出

{
  "readingS": "test"
}

元素的顺序可以改变,可以是第 2 个或第 3 个占位符。该列表最多包含 3 个元素。 我的想法是检查最后一个或中间的元素,但这行不通。该列表是在 modify-overwrite-beta 之前生成的。

您有一些以数字结尾的值。所以我们可以匹配所有以数字结尾的值,最后我们有一个真正的字符串。

[
  {
    "operation": "shift",
    "spec": {
      "firstString": {
        "*": {
          "*0": "",
          "*1": "",
          "*2": "",
          "*3": "",
          "*4": "",
          "*5": "",
          "*6": "",
          "*7": "",
          "*8": "",
          "*9": "",
          "*": {
            "$": "readingS"
          }
        }
      }
    }
  }, {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&"
      }
    }
  }
]

您仍然可以使用 modify-overwrite-beta 转换以及 toInteger 转换,例如

[
  //Determine the elements whether toInteger conversion is applicable for them
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "arr": "=(@(1,firstString))",
      "0": "=toInteger(@(1,arr[0]))",
      "1": "=toInteger(@(1,arr[1]))",
      "2": "=toInteger(@(1,arr[2]))"
    }
  },
  //The arrays are generated from the elements which are eligible to
  //conversion, while not for the others
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "arr": { "*": "&" }
    }
  },
  //The arrays are removed, so all numeric ones by reversing
  //key-value pairs
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "@(0)"
      }
    }
  },
  //Reverse back the pairs
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "readingS[#2]"
      }
    }
  },
  //and pick the leftmost element
  {
    "operation": "cardinality",
    "spec": {
      "*": "ONE"
    }
  }
]