'Jolt Transformation put value in the wrong array element

I have a problem with my Jolt transformation but no idea how to fix it. The value is added to the wrong array element and not to the one it belongs to.

That's my JSON :

{
  "Verkaufsprodukt": [
    {
      "Produkt": [
       {
         "Kurzbeschreibung": "A"
       },
       {
         "Kurzbeschreibung": "B",
         "Kondition": [
           {
             "Bezeichnung": "something"
           }
         ]
       }
     ]
   }
  ]
}

This is the spec:

[
  {
    "operation": "shift",
    "spec": {
      "Verkaufsprodukt": {
        "*": {
          "Produkt": {
            "*": {
              "Kurzbeschreibung": "vertragsdetails.deckungen[].bezeichnung",
              "Kondition": {
                "*": {
                  "Bezeichnung": "vertragsdetails.deckungen[&1].kondition"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "vertragsdetails": {
        "deckungen": "MANY"
      }
    }
  }
]

I am expecting this:

{
  "vertragsdetails": {
    "deckungen": [
      {
        "bezeichnung": "A"
      },
      {
        "bezeichnung": "B",
        "kondition": "something"
      }
    ]
  }
}

but got this:

{
  "vertragsdetails": {
    "deckungen": [
      {
        "bezeichnung": "A",
        "kondition": "something"
      },
      {
        "bezeichnung": "B"
      }
    ]
  }
}

Why is the "kondition" adding to the first element and not to the second where it belongs to? can someone help please? Thanks!



Solution 1:[1]

the problem is in the wildcard you are using in the jolt specification, you should go 3 levels up instead of 1, like:

"Bezeichnung": "vertragsdetails.deckungen[&3].kondition"

a json like this for example:

{
  "Verkaufsprodukt": [
    {
      "Produkt": [
        {
          "Kurzbeschreibung": "A"
        },
        {
          "Kurzbeschreibung": "B",
          "Kondition": [
            {
              "Bezeichnung": "something"
            },
            {
              "Bezeichnung": "andere something"
            },
            {
              "Bezeichnung": "noch andere something"
            }
          ]
        },
        {
          "Kurzbeschreibung": "C",
          "Kondition": [
            {
              "Bezeichnung": "x"
            },
            {
              "Bezeichnung": "andere x"
            },
            {
              "Bezeichnung": "noch andere x"
            }
          ]
        }
      ]
    }
  ]
}

will be transformed into this:

{
  "vertragsdetails" : {
    "deckungen" : [ {
      "bezeichnung" : "A"
    }, {
      "bezeichnung" : "B",
      "kondition" : [ "something", "andere something", "noch andere something" ]
    }, {
      "bezeichnung" : "C",
      "kondition" : [ "x", "andere x", "noch andere x" ]
    } ]
  }
}

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 ΦXocę 웃 Пepeúpa ツ