简体   繁体   中英

Jolt transformation of json - how to add shift operation for null value

I have three scenario which are:

Scenario 1: When "testInt" == 10 then "isTrue" should be set to false and "testInt" to 0 and "testString" as it is.

Input

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

Expected output

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

Scenario 2: When "testInt" == null then "testInt" should be removed and others as it is.

Input

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

Expected output

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

Scenario 3: When "testInt" != 10 (also not null ) then no changes.

Input

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

Expected output

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

It will be helpful if someone suggest me how to achieve these through jolt shift operation.

You can define such a shift operation along with default operation in order to be able to handle null cases through "null" to null conversion

[{
"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"
                }
            }
        }

    }
}]

where @(integer,key) such as "@(2,testInt)" or "@(3,isTrue)" representing the level going up to start search for the needed key presented as the second argument. That can be calculated by counting opening curly braces after "spec": { except for this first { within "spec": { .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM