Search code examples
jsonjolt

integrating the objects of one array into a second array with JOLT


I have the following json (source) and I would like to transform it into the target json. All objects of array "temp" should be integrated in "attributes".

Everytime I try to insert an object to "attributes" I create an array within an array.

Any Help is highly appreciated. I think there is a quick fix, but I haven't found it after hours of research

Source

{
  "items": [
    {
      "itemNo": "abc-4711",
      "attributes": [
        {
          "id": "length_lanyard",
          "type": "float",
          "values": [
            {
              "value": "0.40",
              "unit_of_measure": "m"
            }
          ]
        },
        {
          "id": "rope_length",
          "type": "float",
          "values": [
            {
              "value": "0.40",
              "unit_of_measure": "m"
            }
          ]
        }
      ],
      "temp": [
        {
          "id": "length_lanyard_imp",
          "type": "float",
          "imp_length_lanyard": {
            "values": [
              {
                "value": 1.30,
                "unit_of_measure": "feet"
              }
            ]
          }
        },
        {
          "id": "rope_lanyard_imp",
          "type": "float",
          "imp_length_lanyard": {
            "values": [
              {
                "value": 1.30,
                "unit_of_measure": "feet"
              }
            ]
          }
        }
      ]
    }
  ]
}

target

{
  "items": [
    {
      "itemNo": "abc123",
      "attributes": [
        {
          "id": "length_lanyard",
          "type": "float",
          "values": [
            {
              "value": "0.40",
              "unit_of_measure": "m"
            }
          ]
        },
        {
          "id": "rope_length",
          "type": "float",
          "values": [
            {
              "value": "0.40",
              "unit_of_measure": "m"
            }
          ]
        },
        {
          "id": "length_lanyard_imp",
          "type": "float",
          "imp_length_lanyard": {
            "values": [
              {
                "value": 1.30,
                "unit_of_measure": "feet"
              }
            ]
          }
        },
        {
          "id": "rope_lanyard_imp",
          "type": "float",
          "imp_length_lanyard": {
            "values": [
              {
                "value": 1.30,
                "unit_of_measure": "feet"
              }
            ]
          }
        }
      ]
    }
  ]
}

Solution

  • You can use the following shift transformation spec :

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": "&2[&1].&",
              "attributes|temp": { //accumulate under common node, namely "attributes" 
                "*": {
                  "*": "&4[&3].attributes.@1,id.&"//presumingly ids are unique for the whole JSON
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": "&2[&1].&",
              "attributes": { //reindex the stuff under the common node
                "*": "&3[&2].&1"
              }
            }
          }
        }
      }
    ]
    

    the demo on the site Jolt Transform Demo Using v0.1.1 is :

    Jolt Transform Demo