Search code examples
jsonspecificationsjolt

JOLT SPEC: Shift into existing array


Currently I have the following INPUT JSON. The "temp"-Object has been added with the help of a default spec.

{
  "items": [
    {
      "description": "myDescription",
      "attributes": [
        {
          "identifier": "colour",
          "type": "attribute",
          "values": [
            "DE"
          ],
          "hint": "1"
        },
        {
          "identifier": "pin",
          "type": "attribute",
          "values": [
            "4711"
          ],
          "hint": "1"
        },
        {
          "identifier": "price",
          "type": "price",
          "values": [
            "77.27"
          ],
          "hint": "1"
        },
        {
          "identifier": "weight",
          "type": "attribute",
          "values": [
            "0.47"
          ],
          "hint": "1"
        },
        {
          "identifier": "status",
          "type": "attribute",
          "values": [
            "active"
          ],
          "hint": "1"
        }
      ]
    }
  ],
  "temp": {
    "hint": "1",
    "identifier": "additionalDescription",
    "values": [
      {
        "lang": "de",
        "value": "temp"
      },
      {
        "lang": "en",
        "value": "temp"
      },
      {
        "lang": "fr",
        "value": "temp"
      },
      {
        "lang": "it",
        "value": "temp"
      },
      {
        "lang": "es",
        "value": "temp"
      }
    ],
    "type": "attribute"
  }
}

Desired Output:

{
  "items": [
    {
      "description": "myDescription",
      "attributes": [
        {
          "identifier": "colour",
          "type": "attribute",
          "values": [
            "DE"
          ],
          "hint": "1"
        },
        {
          "identifier": "pin",
          "type": "attribute",
          "values": [
            "4711"
          ],
          "hint": "1"
        },
        {
          "identifier": "price",
          "type": "price",
          "values": [
            "77.27"
          ],
          "hint": "1"
        },
        {
          "identifier": "weight",
          "type": "attribute",
          "values": [
            "0.47"
          ],
          "hint": "1"
        },
        {
          "identifier": "status",
          "type": "attribute",
          "values": [
            "active"
          ],
          "hint": "1"
        },
        {
          "hint": "1",
          "identifier": "additionalDescription",
          "values": [
            {
              "lang": "de",
              "value": "myDescription"
            },
            {
              "lang": "en",
              "value": "myDescription"
            },
            {
              "lang": "fr",
              "value": "myDescription"
            },
            {
              "lang": "it",
              "value": "myDescription"
            },
            {
              "lang": "es",
              "value": "myDescription"
            }
          ],
          "type": "attribute"
        }
      ]
    }
  ]
}

The spec that I am looking for should fullfill two requirements:

  1. integrate the "new" object within the attributes array.
  2. replace the value "temp" by the value of the description field, which can be found at the top of the json.

Unfortunately I already struggle with getting the arrays correctly into the output JSON, not to mention the Overwriting of the value "temp"


[
  {
    "operation": "shift",
    "spec": {
      "items": {
        "*": {
          "description": "&2.myDescription",
          "attributes": {
            "@(3,temp)": "&3.&2.&",
            "*": "&3.&2.&"
          }
        }
      }
    }
  }
]

Any help are highly appreciated. Also, I would be happy to receive hints regarding the first requirement as well. I always struggle when building the RHS (right-hand side) whenever arrays are involved.


Solution

  • What you need is to bring the desired attributes value after 4 levels traversing the tree starting from the the value attribute's level within a modify transformation spec along with overwrite option such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "temp": {
            "values": {
              "*": {
                "value": "@(4,items[0].description)"
              }
            }
          }
        }
      }
    ]