Search code examples
arraysjsontransactionsjolt

JOLT split and array data


I have a JSON like this

[
  {
    "MasterUrl": "https://$$$$/exodus2022/player.aspx",
    "Programme": [
      {
        "Retirement": "2022",
        "Current Team": "Bulls",
        "Previous Team 1": "NA",
        "Previous Team 2": "NA",
        "Previous Team 3": "NA"
      },
      {
        "Retirement": "2020",
        "Current Team": "Celtics",
        "Previous Team 1": "NA",
        "Previous Team 2": "NA",
        "Previous Team 3": "NA"
      }
    ]
  },
  {
    "MasterUrl": "https://$$$$/risen2022/player.aspx",
    "Programme": [
      {
        "Current Team": "Heat",
        "Current Location": "Miami",
        "Previous Team 1": "NA",
        "Previous Team 2": "NA",
        "Previous Team 3": "NA"
      },
      {
        "Current Team": "Cavalliers",
        "Current Location": "Texas",
        "Previous Team 1": "NA",
        "Previous Team 2": "NA",
        "Previous Team 3": "NA"
      },
      {
        "Current Team": "Pistons",
        "Current Location": "New York",
        "Previous Team 1": "NA",
        "Previous Team 2": "NA",
        "Previous Team 3": "NA"
      }
    ]
  }
]

And I need to split the information into 2 arrays, one for the ones under the @exodus2022 Masterurl and the other for the @risen2022 Masterurl. Also, I need to remove the player.aspx from both the urls and the previous team data.

The result should be something like this:

[
  {
    "Exodus2022": [
      {
        "MasterUrl": "https://$$$$/exodus2022",
        "Programme": [
          {
            "Retirement": "2022",
            "Current Team": "Bulls"
          },
          {
            "Retirement": "2020",
            "Current Team": "Celtics"
          }
        ]
      }
    ]
  },
  {
    "Risen2022": [
      {
        "MasterUrl": "https://$$$$/risen2022",
        "Programme": [
          {
            "Current Team": "HEAT",
            "Current Location": "Miami"
          },
          {
            "Current Team": "Cavalliers",
            "Current Location": "Texas"
          },
          {
            "Current Team": "Pistons",
            "Current Location": "New York"
          }
        ]
      }
    ]
  }
]

Can this transformation be done with a JOLT?


Solution

  • You can use the following Jolt spec:

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "MasterUrl": "=split('/player.aspx', @(1,MasterUrl))",
            "MasterUr*": "=lastElement"
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "key": "=split('https://\\$\\$\\$\\$/', @(1,MasterUrl))",
            "ke*": "=lastElement"
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "key": "=split('', @(1,key))"
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "key": {
              "[0]": "=toUpper(@(0))"
            },
            "ke*": "=join('',@0)"
          }
        }
        },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "MasterUrl|Programme": "[&1].@(1,key).&"
          }
        }
      },
      {
        "operation": "remove",
        "spec": {
          "*": {
            "*": {
              "Programme": {
                "*": {
                  "Previous*": ""
                }
              }
            }
          }
        }
      }
    ]
    

    Without lastElement

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "MasterUrl": "=split('/player.aspx', @(1,MasterUrl))"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "[&1].&",
            "MasterUrl": {
              "0": "[&2].&1"
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "key": "=split('https://\\$\\$\\$\\$/', @(1,MasterUrl))"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "[&1].&",
            "key": {
              "1": "[&2].&1"
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "key": "=split('', @(1,key))"
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "key": {
              "[0]": "=toUpper(@(0))"
            },
            "ke*": "=join('',@0)"
          }
        }
        },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "MasterUrl|Programme": "[&1].@(1,key).&"
          }
        }
      },
      {
        "operation": "remove",
        "spec": {
          "*": {
            "*": {
              "Programme": {
                "*": {
                  "Previous*": ""
                }
              }
            }
          }
        }
      }
    ]