JOLT 转换:替换文本

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

我正在尝试寻找转换以下 JSON 数据的解决方案。主要是我希望将带有“/”的日期替换为“-”。

我在下面尝试的替换逻辑不起作用。有人可以帮我吗?

用“modify-overwrite-beta”替换似乎不起作用。

输入JSON

{
  "certificates": [
    {
      "name": "name1",
      "description": "description1",
      "mapping": [
        {
          "date": "DEC/2023",
          "code": "ABC1",
          "rating": null,
          "scope": [
            "scope1",
            "scope2",
            "scope3"
          ]
        }
      ]
    },
    {
      "name": "name2",
      "description": "description2",
      "mapping": [
        {
          "date": null,
          "code": "ABC1",
          "rating": null,
          "scope": [
            "TBD"
          ]
        }
      ]
    }
  ]
}

预期输出:

{
  "certificates" : [ {
    "name" : "name1",
    "description" : "description1",
    "validity" : "DEC-2023",
    "code" : "ABC1",
    "rating" : null,
    "scope" : [ "scope1", "scope2", "scope3" ]
  }, {
    "name" : "name2",
    "description" : "description2",
    "validity" : null,
    "code" : "ABC1",
    "rating" : null,
    "scope" : [ "TBD" ]
  } ],
  "code" : "ABC1"
}

尝试了以下规格

[
  {
    "operation": "shift",
    "spec": {
      "certificates": {
        "*": {
          "name": "certificates[&1].name",
          "description": "certificates[&1].description",
          "mapping": {
            "*": {
              "*": "certificates[&3].&",
              "date": "certificates[&3].validity"
            }
          }
        }
      }
    }
  }, {
    "operation": "default",
    "spec": {
      "code": "ABC1"
    }
  }, {
    "operation": "modify-overwrite-beta",
    "spec": {
      "certificates": {
        "*": {
          "validity": "=replace(validity,'/','-')"
        }
      }
    }
  }
]

低于输出

arrays json transformation jolt
1个回答
0
投票

希望以下 Jolt 规格对您有所帮助。

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "certificates": {
        "*": {
          "mapping": {
            "*": {
              "isDateNotNull": ["=notNull(@(1,date))", false],
              "splitDate": "=split('/',@(1,date))",
              "customDate": "=concat(@(1,splitDate[0]), '-',@(1,splitDate[1]) )"
            }
          }
        }
      },
      "code": "ABC1"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "certificates": {
        "*": {
          "*": "certificates[&1].&",
          "mapping": {
            "*": {
              "isDateNotNull": {
                "false": {
                  "@(2,date)": "certificates[&5].validity"
                },
                "*": {
                  "@(2,customDate)": "certificates[&5].validity"
                }
              },
              "*": "certificates[&3].&"
            }
          }
        }
      },
      "*": "&"
    }
  },
  {
    "operation": "remove",
    "spec": {
      "certificates": {
        "*": {
          "date": "",
          "customDate": "",
          "splitDate": ""
        }
      }
    }
  }
]

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