2

I want to use a jolt transformation to add an element to an array.

My approach is to use default to append to the last element in the array

Input

{
  "options": [
    {
      "name": "Will",
      "state": "enabled"
    },
    {
      "name:": "Bert",
      "state": "enabled"
    },
    {
      "name": "Kate",
      "state": "disabled"
    }
  ]
}

Jolt Spec

[
  {
    "operation": "default",
    "spec": {
      "options[]": {
        "3": {
          "name": "Bob",
          "state": "enabled"
        }
      }
    }
  }
]

Desired Output

{
  "options": [
    {
      "name": "Will",
      "state": "enabled"
    },
    {
      "name": "Bert",
      "state": "enabled"
    },
    {
      "name": "Kate",
      "state": "disabled"
    },
    {
      "name": "Bob",
      "state": "enabled"
    }
  ]
}

It works if the input array length is 3. How can I obtain the array length and set the index dynamically?

1 Answer 1

4

A little hokey, but possible.

Spec

[
  {
    // default in the new "thing first"
    "operation": "default",
    "spec": {
      "temp": {
        "name": "Bob",
        "state": "enabled"
      }
    }
  },
  {
    // copy the options array across first, 
    //  then copy the value (map with Bob) to "options"
    //  which is an array, so Shift will add it to the end
    "operation": "shift",
    "spec": {
      "options": "options",
      "temp": "options"
    }
  }
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.