首页 > 解决方案 > json的颠簸转换-如何为空值添加移位操作

问题描述

我有三个场景:

场景 1:应何时"testInt" == 10将 then"isTrue"设置为false"testInt"to0"testString"as it is。

输入

{
"testString" :"testValue",
"testInt": 10,
"isTrue": true
}

预期产出

{
"testString" :"testValue",
"testInt": 0,
"isTrue": false
}

场景 2:什么时候应该删除 then 而其他的应该被删除"testInt" == null"testInt"

输入

{
"testString" :"testValue",
"testInt": null,
"isTrue": true
}

预期产出

{
"testString" :"testValue",
"isTrue": true
}

场景 3:"testInt" != 10(also not null) 然后没有变化。

输入

{
"testString" :"testValue",
"testInt": 20,
"isTrue": true
}

预期产出

{
"testString" :"testValue",
"testInt": 20,
"isTrue": true
}

如果有人建议我如何通过颠簸操作来实现这些,那将会很有帮助。

标签: jsontransformationjolt

解决方案


您可以将此类shift操作与default操作一起定义,以便能够通过转换来处理null案例"null"null

[{
"operation": "default",
"spec": {
    "testInt": "null"
}
},{
    "operation": "shift",
    "spec": {
        "testString": "testString",
        "testInt": {
            "10": {
                "#0": "testInt"
            },
            "null": null,
            "*": {
                "@(2,testInt)": "testInt"
            }

        },
        "isTrue": {
            "@(2,testInt)": {
                "10": {
                    "#false": "isTrue"
                },
                "*": {
                    "@(3,isTrue)": "isTrue"
                }
            }
        }

    }
}]

@(integer,key)例如"@(2,testInt)"或表示"@(3,isTrue)"要开始搜索作为第二个参数呈现的所需键的级别。"spec": {这可以通过在除了 this first {within之后计算打开的花括号来计算"spec": {


推荐阅读