'How to append extra fields to list in JOLT?

I'm trying to add some extra fields to the list which are not there in the input json. I can add fields if it's an object, but I'm not able to add the fields to an array. Please, someone help me write a specification.

input JSON is:

[
  {
    "List": [
      {
        "ITEM_NO": "abcd"
      }
    ]
  }
]

written a spec file

Spec File is:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "List": {
          "0": {
            "ITEM_NO": "risk[0].one"
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "risk": [
        {
          "two": "efgh",
          "three": "ijkl"
        }
      ]
    }
  }
]

But output is not like expected

expected output is:

{
  "risk": [
    {
      "one": "abcd",
      "two": "efgh",
      "three": "ijkl"
    }
  ]
}

How to add extra fields?



Solution 1:[1]

Spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "List": {
          "0": {
            "ITEM_NO": "risk[0].one"
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "risk[]": {
        "0": {
          "two": "efgh",
          "three": "ijkl"
        }
      }
    }
  }
]

Produces the output you want, but not sure what you are trying to do.

Solution 2:[2]

You can figure it out by using a single shift transformation spec such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "List": {
          "*": {
            "*": "r[&1].one",
            "#efgh": "r[&1].two",
            "#ijkl": "r[&1].three"
          }
        }
      }
    }
  }
]

where you can hard code desired strings to be placed as a value in the output through use of "#" wildcard, and "*" to represent all attributes(in this case the only attribute is the one with key name "one").

the demo on the site http://jolt-demo.appspot.com/ is

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Barbaros Özhan
Solution 2 Barbaros Özhan