skip to Main Content

I have this JSON as input:

{
  "my_array": []
}

If the array is empty I need to add a placeholder to the array, so this is the desired output:

{
  "my_array": ["placeholder"]
}

If the array has a value/s:

{
  "my_array": ["I have a value", "I am a second value"]
}

I need to keep those values on the array, so the desired output should be the equal to the input:

{
  "my_array": ["I have a value", "I am a second value"]
}

So far I have tried using the default operation without success:

[
  {
    "operation": "default",
    "spec": {
      "my_array[]": [
        "placeholder"
      ]
    }
  }
]

And I have also tried using a conditional with the shift operation without success:

[
  {
    "operation": "shift",
    "spec": {
      "my_array": {
        "[]": {
          "#placeholder": "my_array"
        },
        "*": {
          "@(2,my_array)": "my_array"
        }
      }
    }
  }
]

This last code throws the error:

Error running the Transform.
JOLT Chainr encountered an exception constructing Transform className:com.bazaarvoice.jolt.Shiftr at index:0.

2

Answers


  1. Chosen as BEST ANSWER

    The first answer provided Barbaros Özhan works correctly. But if you have a jolt library verion that does not support the size function, this solution should work as well:

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "my_array_joined": "=join('',@(1,my_array))"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "my_array_joined": {
            "": {
              "#placeholder": "my_array"
            },
            "*": {
              "@(2,my_array)": "my_array"
            }
          }
        }
      }
    ]
    
    

    Edit: The second solution provided by Barbaros Özhan works as well


  2. You can handle the issue through measuring the size of the array and put into the conditional whether it’s equal to zero or not such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "sz_array": "=size(@(1,my_array))"
        }
     },
     {
        "operation": "shift",
        "spec": {
          "sz_array": {
            "0": {
              "@2,my_array": { "#placeholder": "my_array[]" }
            },
            "*": {
              "@2,my_array": "my_array"
            }
          }
        }
      }
    ]
    

    Edit : Considering that you have the older versions of the Jolt library, you can prefer using the following spec as an alternative

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "xyz": ["=toInteger(@(1,my_array))", ["0"]]
        }
      },
      {
        "operation": "shift",
        "spec": {
          "xyz": {
            "*": {
              "0": {
                "@3,my_array": { "#placeholder": "my_array[]" }
              },
              "*": {
                "@3,my_array": "my_array"
              }
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search