简体   繁体   中英

JSON jolt null value added to a nested array after transformation

I have an issue with the JSON jolt transformation, please consider the following case, I have tried different versions of jolt library too.

input json:

{
    "id": "b08a2c93-f59e-4f54-8e16-2f1c4345b53b",
    "equipment": [{
        "number": "S765769",
        "type": "40 DRY"
    }, {
        "number": "N5765769",
        "type": "20 DRY"
    }]
}

Jolt Spec

[{
    "operation": "shift",
    "spec": {
        "equipment": {
            "*": {
                "number": ["CTN[&1].IDT", "CTN[&1].CTN[&1].IDT"],
                "type": "CTN[&1].CTN[&1].TYP"
            }
        }
    }
}]

Output Json:

{
    "CTN": [{
        "IDT": "S765769",
        "CTN": [{
            "IDT": "S765769",
            "TYP": "40 DRY"
        }]
    }, {
        "IDT": "N5765769",
        "CTN": [ ** null ** , {
            "IDT": "N5765769",
            "TYP": "20 DRY"
        }]
    }]
}

As you can see in the output, in the second CTN array object, the first element is null ! How do we get rid of that null so that the output will be like this

Required output

{
    "CTN": [{
        "IDT": "S765769",
        "CTN": [{
            "IDT": "S765769",
            "TYP": "40 DRY"
        }]
    }, {
        "IDT": "N5765769",
        "CTN": [{
            "IDT": "N5765769",
            "TYP": "20 DRY"
        }]
    }]
}

You can squash that null out adding an extra step to your spec:

[{
  "operation": "shift",
  "spec": {
    "equipment": {
      "*": {
        "number": ["CTN[&1].IDT", "CTN[&1].CTN[&1].IDT"],
        "type": "CTN[&1].CTN[&1].TYP"
      }
    }
  }
}, {
  "operation": "modify-overwrite-beta",
  "spec": {
    "*": "=recursivelySquashNulls"
  }
}]

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