Search code examples
jsonjolt

Convert condition's values to integer in JOLT transformation


Using jolt spec transformation i want to convert the condition's values to integer after the conversion.

i have the input JSON

{
  "MainObj": {
    "SubObj1": {
      "SubArr1": [
        {
          "SubArr2": [
            {
              "Details": [
                {
                  "IsTrue": "Y",
                  "Name": "ABC"
                },
                {
                  "IsTrue": "N",
                  "Name": "DEF"
                }
              ]
            }
          ]
        },
        {
          "SubArr2": [
            {
              "Details": [
                {
                  "IsTrue": "N",
                  "Name": "GHI"
                },
                {
                  "IsTrue": "N",
                  "Name": "JKL"
                }
              ]
            }
          ]
        }
      ]
    }
  }
}

The spec i am using

[
  {
    "operation": "shift",
    "spec": {
      "MainObj": {
        "SubObj1": {
          "SubArr1": {
            "*": {
              "SubArr2": {
                "*": {
                  "Details": {
                    "*": {
                      "Name": "transformedArr1[&5].transformedArr2[&3].details[&1].NAME",
                      "IsTrue": {
                        "Y": {
                          "#1": "transformedArr1[&7].transformedArr2[&5].details[&3].IS_TRUE"
                        },
                        "*": {
                          "#0": "transformedArr1[&7].transformedArr2[&5].details[&3].IS_TRUE"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "IS_TRUE": "=toInteger"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "@": "outputs[]"
    }
  }
]

Ouput that i am getting.

{
  "outputs": [
    {
      "transformedArr1": [
        {
          "transformedArr2": [
            {
              "details": [
                {
                  "NAME": "ABC",
                  "IS_TRUE": "1"
                },
                {
                  "NAME": "DEF",
                  "IS_TRUE": "0"
                }
              ]
            }
          ]
        },
        {
          "transformedArr2": [
            {
              "details": [
                {
                  "NAME": "GHI",
                  "IS_TRUE": "0"
                },
                {
                  "NAME": "JKL",
                  "IS_TRUE": "0"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Desired output:

{
  "outputs": [
    {
      "transformedArr1": [
        {
          "transformedArr2": [
            {
              "details": [
                {
                  "NAME": "ABC",
                  "IS_TRUE": 1
                },
                {
                  "NAME": "DEF",
                  "IS_TRUE": 0
                }
              ]
            }
          ]
        },
        {
          "transformedArr2": [
            {
              "details": [
                {
                  "NAME": "GHI",
                  "IS_TRUE": 0
                },
                {
                  "NAME": "JKL",
                  "IS_TRUE": 0
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

In the output that i am getting i want the value of "IS_TRUE" to be integer. The spec i am using is not converting into integer.

I was able to convert into integer without the conditions. Is there any way that i can convert values to integer in conditions after "shift" operation?


Solution

  • The depth of the attribute within nested objects is not right. You can convert the current modify transformation to

      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {//the level of "transformedArr1"
            "*": {//the level of the indexes "transformedArr1"
              "*": {//the level of "transformedArr2"
                "*": {//the level of the indexes "transformedArr2"
                  "*": {//the level of "details"
                    "*": {//the level of the indexes "details"
                      "IS_TRUE": "=toInteger"
                    }
                  }
                }
              }
            }
          }
        }
      }
    

    eg. 6 layers needed to reach the level of the "IS_TRUE" attribute