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

yjghlzjz  于 2023-01-18  发布在  其他
关注(0)|答案(2)|浏览(126)

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

  • 输入值 *:
{
  "firstString": [
    "0.20",
    "test",
    "0.30"
  ]
}

{
  "firstString": [
    "0.20",
    "0,30",
    "test"
  ]
}
  • 预期产出 *:
{
  "readingS": "test"
}

元素的顺序可以改变,无论是第二个还是第三个占位符。列表最多有3个元素。我的想法是检查最后一个或中间的元素,但是这不起作用。列表是之前用修改-覆盖-测试版生成的。

sh7euo9m

sh7euo9m1#

您仍然可以将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"
    }
  }
]

g6ll5ycj

g6ll5ycj2#

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

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

相关问题