Update json attribute with a condition in jolt

Viewed 190

I have a problem, I don't understand how to update an attribute with a condition in jolt. For example, I have an Object with an inner array of Items. I need to update an Item attribute if another Item attribute equals to something and to return the Object.

Input:

{
  "object": {
    "id": "3cf1543e-be4d-11eb-84c0-87ba01ce01e0",
    "a": "abc",
    "del_sign": false,
    "items": [
      {
        "id": "111",
        "del_sign": false
      },
      {
        "id": "222",
        "del_sign": false
      },
      {
        "id": "333",
        "del_sign": false
      }
    ],
    "b": [],
    "c": []
  }
}

I need:

{
  "object": {
    "id": "3cf1543e-be4d-11eb-84c0-87ba01ce01e0",
    "a": "abc",
    "del_sign": false,
    "items": [
      {
        "id": "111",

        // here changes to true
        "del_sign": true
      },
      {
        "id": "222",
        "del_sign": false
      },
      {
        "id": "333",
        "del_sign": false
      }
    ],
    "b": [],
    "c": []
  }
}

My current jolt spec:

[
  {
    "operation": "shift",
    "spec": {
      "object": {
        "items": {
          "*": {
            "id": {
              "111": {
                "@2": {
                  "#true": "del_sign",
                  "$1": "&3"
                }
              }
            }
          }
        }
      }
    }
  }
]
1 Answers

You can use two step of shift transformations.

Determine the related del_sign value within a conditional as having two arrays for id and del_sign within the same object(items), and properly format them within the second step such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&",
        "items": {
          "*": {
            "id": {
              "111": {
                "@(2,&1)": "&4.&3.id",
                "#true": "&4.&3.del_sign"
              },
              "*": {
                "@1": "&4.&3.&2",
                "@(2,del_sign)": "&4.&3.del_sign"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "items": {
        "*": {
          "*": "&2.[#2].&"
        }
      }
    }
  }
]

Edit : If there are more attributes other than the current ones(id,del_sign), then prefer using the following code in order not to individually repeat the each key such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&",
        "items": {
          "*": {
            "id": {
              "111": {
                "@2": {
                  "#true": "&5.&2.del_sign",
                  "*": "&5.&1.&"
                }
              },
              "*": {
                "@2": "&4.&3"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "items": {
        "*": {
          "del_sign": "ONE"
        }
      }
    }
   },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "items": {
        "*": {
          "*": "&2.[#2].&"
        }
      }
    }
  }
]
Related