Jolt 映射处理 null 和空字符串

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

Jolt 映射处理 null 和空值。

场景1:如果“tempname”不为空,则将name的值设置为tempName。

场景2:如果“tempname”为空,则保留默认值名称而不更改。

场景3:如果“tempname”为空字符串(“”),则保留默认值名称而不更改

场景4:但是如果“tempname”不存在,则保留默认值名称

除了第一个场景,后三个场景都不起作用。如果代码被修改了,最后三个有效,第一个场景就会失败。

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "A": {
        "B": {
          "*": {
            "name": "=firstNotNull(@(1,tempName), @(2,name))"
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "A": {
        "B": {
          "*": {
            "tempName": ""
          }
        }
      }
    }
  }
]

O/p:

{
  "A" : {
    "B" : [ {
      "name" : "rq3ewas"
    }, {
      "name" : null
    }, {
      "name" : ""
    }, {
      "name" : "test4"
    } ]
  }
}

预计O/p:

{
  "A" : {
    "B" : [ {
      "name" : "rq3ewas"
    }, {
      "name" : "test2"
    }, {
      "name" : "test3"
    }, {
      "name" : "test4"
    } ]
  }
}
[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "A": {
        "B": {
          "*": {
            "name": "=firstNotNull(@(1,tempName), @(2,name))"
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "A": {
        "B": {
          "*": {
            "tempName": ""
          }
        }
      }
    }
  }
]

jolt elvis-operator
1个回答
0
投票

原则上,您应该摆脱 空字符串,因为可以通过修改规范来操作 null 值属性。针对此目标,可以使用以下转换:

[
  { //concert "" valued "tempName" attribute's value to null,
    //while removing the null valued ones(doesn't matter)
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "tempName": {
              "": "&4.&3[&2].&1", //convert "" valued "tempName" attribute to to the one with null value
              "*": {
                "@1": "&5.&4[&3].&2"
              }
            },
            "*": "&3.&2[&1].&"
          }
        }
      }
    }
  },
  { //set "name" attributes by the values of the 
    //"tempName" attributes provided that they're non-null  
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "*": {
            "name": "=notNull(@(1,tempName))"
          }
        }
      }
    }
  },
  { //keep the "name" attributes only
    "operation": "remove",
    "spec": {
      "*": {
        "*": {
          "*": {
            "tempName": ""
          }
        }
      }
    }
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.