Search code examples
jsonjolt

normalize a JSON array with jolt


I would like to "normalize" a JSON array of items with the help of jolt.

The source json would be like (number and content of attributes may vary)

[
  {
    "itemNo": 1012113,
    "item_original_no": "item1",
    "productNo": "prod1",
    "adjusting_buckles": "adjusting_buckles_shoulders | adjusting_buckles_hips | adjusting_buckles_legs",
    "anchor_point_type": null,
    "anti_twist": null,
    "attachment_points": "attachment_points_sternal_aluminium | attachment_points_dorsal_aluminium | attachment_points_side_aluminium | attachment_points_climbing_protection_aluminium",
    "attachment_points_bottom": null,
    "attachment_points_top": null,
    "backstop": null,
    "battery_range": null
  },
  {
    "itemNo": 1012108,
    "item_original_no": "item2",
    "productNo": "prod2",
    "adjusting_buckles": "adjusting_buckles_shoulders | adjusting_buckles_hips | adjusting_buckles_legs",
    "anchor_point_type": null,
    "anti_twist": null,
    "attachment_points": "attachment_points_sternal_aluminium | attachment_points_dorsal_aluminium | attachment_points_side_aluminium | attachment_points_climbing_protection_aluminium",
    "attachment_points_bottom": null,
    "attachment_points_top": null,
    "backstop": null,
    "battery_range": null
  }
]

in my expected json the itemNo should serve as an id.Each key should be the value of key "attribute" and each value should be the value of key "value".

[ 
    {"id":1012113,"attribute":"item_original_no","value":"item1"},
    {"id":1012113,"attribute":"productNo","value":"product1"},
    {"id":1012113,"attribute":"adjusting_buckles","value":"adjusting_buckles_shoulders | adjusting_buckles_hips | adjusting_buckles_legs"},
    {"id":1012113,"attribute":"anchor_point_type","value":null},
    {"id":1012113,"attribute":"anti_twist","value":null},
    {"id":1012113,"attribute":"attachment_points","value":"attachment_points_sternal_aluminium | attachment_points_dorsal_aluminium | attachment_points_side_aluminium |  attachment_points_climbing_protection_aluminium"},
    {"id":1012113,"attribute":"attachment_points_bottom","value":null},
    {"id":1012113,"attribute":"attachment_points_top","value":null},
    {"id":1012113,"attribute":"backstop","value":null},
    {"id":1012113,"attribute":"battery_range","value":null},
    {"id":1012108,"attribute":"item_original_no","value":"item2"},
    {"id":1012108,"attribute":"productNo","value":"product2"},
    {"id":1012108,"attribute":"adjusting_buckles","value":"adjusting_buckles_shoulders | adjusting_buckles_hips | adjusting_buckles_legs"},
    {"id":1012108,"attribute":"anchor_point_type","value":null},
    {"id":1012108,"attribute":"anti_twist","value":null},
    {"id":1012108,"attribute":"attachment_points","value":"attachment_points_sternal_aluminium | attachment_points_dorsal_aluminium | attachment_points_side_aluminiumattachment_points_climbing_protection_aluminium"},
    {"id":1012108,"attribute":"attachment_points_bottom","value":null},
    {"id":1012108,"attribute":"attachment_points_top","value":null},
    {"id":1012108,"attribute":"backstop","value":null},
    {"id":1012108,"attribute":"battery_range","value":null}
]

Frankly spoken my approach so far is very poor, when it commes to attributes and values.

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "itemNo": "result.[&1].id",
        "*": "result.[&1].value",
        "$": "result.[&1].attribute"
      }
    }
  }
]

Therefory any hint and help would be highly appreciated. Thanks in advance


Solution

  • You can go one more level deeper to match the pairs while grouping the values by itemNo values and their respective keys for each such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "@1,itemNo": "@2,itemNo.&.id",
              "$": "@2,itemNo.&.attribute",
              "@": "@2,itemNo.&.value"
            }
          }
        }
      },
      { //get rid of the object keys
        "operation": "shift",
        "spec": {
          "*": {
            "*": "[]"
          }
        }
      }
    ]