head 的 Jolt 替代方案可在 json 数组中查找第一个非空非空字符串

问题描述 投票:0回答:1

是否有任何替代方法

head
来转换并从 JSON 数组获取第一个非空或非空值。类似于
head(/part/subline/sublineGrouop/sublineGroupTP/detailsGP/dtlLine/details/code)
应该返回
US

{
  "part": {
    "subline": {
      "sublineGroup": {
        "property1": "test",
        "proerty2": "tes2",
        "sublineGroupTP": {
          "detailsGP": [
            {
              "dtlLine": {
                "details": {
                  "code": null
                }
              }
            },
            {
              "dtlLine": {
                "details": {
                  "code": ""
                }
              }
            },
            {
              "dtlLine": {
                "details": {
                  "code": "US"
                }
              }
            },
            {
              "dtlLine": {
                "details": {
                  "code": "UK"
                }
              }
            }
          ]
        }
      }
    }
  }
}

预期输出 JSON

{
  "countryCode": "US"
}
json transformation jolt
1个回答
0
投票

您可以使用以下shift转换。

[
  {
    "operation": "shift",
    "spec": {
      "part": {
        "subline": {
          "sublineGroup": {
            "sublineGroupTP": {
              "detailsGP": {
                "*": {
                  "dtlLine": {
                    "details": {
                      "code": {
                        "": "", //Skip null and empty values.
                        "*": {
                          "@(2,&1)": "countryCode" //Group non-null values
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "countryCode": {
        "0": "&1" // 1st item in the array.
      }
    }
  }
]

© www.soinside.com 2019 - 2024. All rights reserved.