admin管理员组

文章数量:1316617

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"
              }
            ]
          }
        }
      ]
    }
  ]
}

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"
              }
            ]
          }
        }
      ]
    }
  ]
}
Share Improve this question asked Jan 30 at 14:08 Katja BürgerKatja Bürger 351 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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 :

本文标签: jsonintegrating the objects of one array into a second array with JOLTStack Overflow