Search code examples
jsonapache-nifijolt

JOLT Transformation : Convert string to Boolean in JSON Array


I want to replace the string value with a boolean based on the condition. If the IsCompleted value is Yes then the output value should be true if It is No then the output value should be false.

Input

[
  {
    "Id": 1,
    "Name": "Sumit",
    "IsCompleted": "Yes"
  },
  {
    "Id": 2,
    "Name": "Sumit",
    "IsCompleted": "No"
  }
]

JOLT Spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "IsCompleted": {
          "Yes": {
            "#true": "IsCompleted"
          },
          "No": {
            "#false": "IsCompleted"
          }
        },
        "*": "&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "IsCompleted": "=toBoolean"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": "[].&"
      }
    }
  }
]

Expected Output

[
  {
    "Id": 1,
    "Name": "Sumit",
    "IsCompleted": true
  },
  {
    "Id": 2,
    "Name": "Sumit",
    "IsCompleted": false
  }
]

Actual Output

[
  {
    "Id": [1, 2]
  },
  {
    "Name": ["Sumit", "Sumit"]
  },
  {
    "IsCompleted": [true, false]
  }
]

Solution

  • I just changed your spec to the true thing that should be. So you can use the following JOLT spec:

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "[&1].&",
            "IsCompleted": {
              "Yes": {
                "#true": "[&3].&2"
              },
              "No": {
                "#false": "[&3].&2"
              }
            }
          }
        }
        },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "IsCompleted": "=toBoolean"
          }
        }
      }
    ]