【问题标题】:Jolt transformation of json - how to add shift operation for null valuejson的颠簸转换-如何为空值添加移位操作
【发布时间】:2021-02-24 17:28:07
【问题描述】:

我有三个场景:

场景 1:"testInt" == 10 时,"isTrue" 应设置为 false"testInt" 应设置为 0"testString"

输入

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

预期输出

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

场景 2:"testInt" == null 时,"testInt" 应该被删除,其他的则保持原样。

输入

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

预期输出

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

场景 3:"testInt" != 10(也是not null)时,没有变化。

输入

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

预期输出

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

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

【问题讨论】:

    标签: json transformation jolt


    【解决方案1】:

    您可以将这样的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": { 之后的左大括号来计算,除了"spec": { 中的第一个{

    【讨论】:

    • 感谢@barbaros-Özhan 的回复,但在场景 2 中,我没有得到预期的输出。我在这个“isTrue”中只得到 {"testString" : "testValue"} 正在被丢弃。
    • 我正在努力解决这个问题,因为在我的情况下,我得到的是 null 而不是“null”。我没有找到任何处理 null 的解决方案,这在 jolt shift 操作中是不可能的吗?
    • @VikramKumar 我已经通过默认操作添加了一个空转换。祝你好运。
    • 再次感谢您的快速回复,但也存在一个限制,即如果任何字段为空,则应将其删除。那是在场景 2 中。
    • 非常感谢@barbaros-Özhan,我终于得到了预期的输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多