bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.54k stars 328 forks source link

Support for recursive parent-child trees #1172

Open MarcelHeemskerk opened 1 year ago

MarcelHeemskerk commented 1 year ago

Suppose this is the input, consisting of nested Persons, which can be arbitrarily deep:

{
  "Name": "Mike",
  "Age": 50,
  "Hobbies": [ 
     { 
        "id": 2,
        "name": "painting"
     } 
  ],
  "Children": [
      {
        "Name": "MikeSon",
        "Age": 30,
        "Hobbies": [ 
          { 
            "id": 1,
            "name": "painting"
         } ,
          { 
            "id": 2,
            "name": "squash"
         } 
        ],
        "Children": [
           {
              "Name": "MikeGrandSon",
              "Age": 10,
              "Hobbies": [],
              "Children": []
           },
           {
              "Name": "MikeGrandDaughter",
              "Age": 5,
              "Hobbies" : [
                  { 
                     "id": 3,
                     "name": "playing"
                  } 
              ],
              "Children": []
           }
         ]
      }
    ]
}

How to transform that with a complex spec acting per Person? For instance, change "Name" into "FirstName" and concatenate "Hobbies" ?

{
  "FirstName": "Mike",
  "Age": 50,
  "Hobbies": "painting",
  "Children": [
      {
        "FirstName": "MikeSon",
        "Age": 30,
        "Hobbies": "painting, squash",
        "Children": [
           {
              "FirstName": "MikeGrandSon",
              "Age": 10,
              "Hobbies: "",
              "Children": []
           },
           {
              "FirstName": "MikeGrandDaughter",
              "Age": 5,
              "Hobbies": "playing",
              "Children": []
           }
         ]
      }
    ]
}

I would like to define a shift spec for a single Person, as complex as needed, and then apply that on all Persons in the tree. The actual transformations can be arbitrarily complex.

The solution in #1114 with recursiveReplacement does not use a shift spec for every Person leave in the tree, it requires programming every change in Java, right?