skip to Main Content

I am pretty new to JOLTtransform and I’m trying to understand it better. I tried alot of stuff but i didnt work out. I try to take a List of JSONS and Split them so i can put it in a Database while with the KH and ref on each TRD field.

Input:

[
  {
    "KH": "12",
    "ref": "1808",
    "TRD": [
      {
        "KATEGORIE": "KH10",
        "NR": 1,
        "DNR": "02209394"
      },
      {
        "KATEGORIE": "KH10",
        "NR": 2,
        "DNR": "00761006"
      },
      {
        "KATEGORIE": "KH20",
        "NR": 3,
        "DNR": "09999092"
      }
    ]
  },
  {
    "KH": "12",
    "ref": "1808",
    "TRD": [
      {
        "KATEGORIE": "KH10",
        "NR": 1,
        "DNR": "01018143"
      },
      {
        "KATEGORIE": "KH20",
        "NR": 2,
        "DNR": "085859973"
      }
    ]
  }
]

And expected Output:

[
  {
    "KH": "12",
    "ref": "1808",
    "NR": 1,
    "KATEGORIE": "KH10",
    "DNR": "02209394"
  },
  {
    "KH": "12",
    "ref": "1808",
    "NR": 2,
    "KATEGORIE": "KH10",
    "DNR": "00761006"
  },
  {
    "KH": "12",
    "ref": "1808",
    "NR": 3,
    "KATEGORIE": "KH20",
    "DNR": "09999092"
  },
  {
    "KH": "12",
    "ref": "1808",
    "NR": 1,
    "KATEGORIE": "KH10",
    "DNR": "01018143"
  },
  {
    "KH": "12",
    "ref": "1808",
    "NR": 2,
    "KATEGORIE": "KH20",
    "DNR": "085859973"
  }
]

Im using :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "TRD": {
          "*": {
            "@(2,KH)": "[#2].KH",
            "@(2,ref)": "[#2].ref",
            "KATEGORIE": "[#2].KATEGORIE",
            "NR": "[#2].NR",
            "DNR": "[#2].DNR"
          }
        }
      }
    }
  }
]

and im getting:

[ 
  {
    "DNR" : [ "02209394", "01018143" ],
    "KATEGORIE" : [ "KH10", "KH10" ],
    "KH" : [ "12", "12" ],
    "NR" : [ 1, 1 ],
    "ref" : [ "1808", "1808" ]
  }, 
  {
    "DNR" : [ "00761006", "085859973" ],
    "KATEGORIE" : [ "KH10", "KH20" ],
    "KH" : [ "12", "12" ],
    "NR" : [ 2, 2 ],
    "ref" : [ "1808", "1808" ]
  }, 
  {
    "DNR" : "09999092",
    "KATEGORIE" : "KH20",
    "KH" : "12",
    "NR" : 3,
    "ref" : "1808"
  } 
]

i dont know where i go wrong. I tried chatGPT but it made it even worse lol

2

Answers


  1. You can use the below spec

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "TRD": {
              "*": {
                "*": "[#4].&1.&",
                "@(2,KH)": "[#4].&1.KH",
                "@(2,ref)": "[#4].&1.ref"
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "[]"
          }
        }
      }
    ]
    
    Login or Signup to reply.
  2. You can apply two different tiers of grouping of indexes as in the following case :

    [
      { // group by the indexes of the outermost objects(eg.&3) 
        //   and by the indexes of the innermost objects(eg.&1) 
        "operation": "shift",
        "spec": {
          "*": {
            "TRD": {
              "*": {
                "@2,KH": "&3_&1.KH",
                "@2,ref": "&3_&1.ref",
                "*": "&3_&1.&"
              }
            }
          }
        }
      },
      { // get rid of the object keys
        "operation": "shift",
        "spec": {
          "*": ""
        }
      }
    ]
    

    the demo on the site http://jolt-demo.appspot.com/ is :

    enter image description here

    or you can handle it more dynamically(eg. without writing each attribute "KH" and "ref" individually) by using the following tranformation

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "[&1].others.&",
            "TRD": "[&1].&"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "TRD": {
              "*": {
                "@2,others": { "*": "&4_&2.&" }, // go two levels up the tree to grab the values from the "others" objects
                "*": "&3_&1.&"
              }
            }
          }
        }
      },
      { // get rid of the object labels
        "operation": "shift",
        "spec": {
          "*": ""
        }
      }
    ]
    

    the demo on the site http://jolt-demo.appspot.com/ is :

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search