Creating a value in json only if the key exists using Jolt
I am new to Jolt, I am trying to transform JSON to a particular format using nifi, where i am moving some objects into another object, I think i am close
This is my JSON
{
"relatedParty": [
{
"id": "B2BSmallandMedium"
}
],
"serviceOrderItem": [
{
"id": "e26205f4-e144-4ded-9e64-82ede0b26f4",
"action": "add",
"service": {
"name": "cust",
"state": "active",
"supportingService": [
{
"id": "e26205f4-e144-4ded-9e64-82ede0b26e99",
"href": "serviceInventory/v4/service"
},
{
"id": "7313c885-a075-4188-a6bc-c3fbfb4c7422",
"href": "activationAndConfiguration/v4/service"
}
]
}
}
]
}
I am new to Jolt, I am trying to transform JSON to a particular format, where i am moving some objects into another object, I think i am close.
This is my JSON
{
"relatedParty": [
{
"id": "B2BSmallandMedium"
}
],
"serviceOrderItem": [
{
"id": "e26205f4-e144-4ded-9e64-82ede0b26f4",
"action": "add",
"service": {
"name": "cust",
"state": "active",
"supportingService": [
{
"id": "e26205f4-e144-4ded-9e64-82ede0b26e99",
"href": "serviceInventory/v4/service"
},
{
"id": "7313c885-a075-4188-a6bc-c3fbfb4c7422",
"href": "activationAndConfiguration/v4/service"
}
]
}
}
]
}
I am using the JOLT
[
{
"operation": "shift",
"spec": {
"relatedParty": "relatedParty",
"serviceOrderItem": {
"*": {
"id": "serviceOrderItem[&1].id",
"action": "serviceOrderItem[&1].action",
"service": {
"*": "serviceOrderItem[&2].service.&",
"#modifyPath": "serviceOrderItem[&2].service.serviceCharacteristic[0].name",
"@(1,modifyPath)": "serviceOrderItem[&2].service.serviceCharacteristic[0].value",
"#supportingService": "serviceOrderItem[&2].service.serviceCharacteristic[1].name",
"@(1,service.supportingService)": "serviceOrderItem[&2].service.serviceCharacteristic[1].value"
}
}
}
}
},
{
"operation": "modify-default-beta",
"spec": {
"serviceOrderItem": {
"*": {
"service": {
"serviceCharacteristic": {
"*": {
"value": []
}
}
}
}
}
}
},
{
"operation": "remove",
"spec": {
"serviceOrderItem": {
"*": {
"service": {
"supportingService": ""
}
}
}
}
}]
this is now giving me
"service" : {
"name" : "cust",
"serviceCharacteristic" : [ {
"name" : "modifyPath",
"value" : "[]"
}, {
"name" : "supportingService",
"value" : [ {
"href" : "serviceInventory/v4/service",
"id" : "e26205f4-e144-4ded-9e64-82ede0b26e99"
}, {
"href" : "activationAndConfiguration/v4/service",
"id" : "7313c885-a075-4188-a6bc-c3fbfb4c7422"
} ]
} ],
"state" : "active"
}
} ]
This is working for me but,
I would like to know if it is possible replace:
"#modifyPath": "serviceOrderItem[&2].service.serviceCharacteristic[0].name"
"#supportingService": "serviceOrderItem[&2].service.serviceCharacteristic[1].name"
to update the Json only when the key is actually available. I do not want an empty array.
Thank you for your time

