Support Questions
Find answers, ask questions, and share your expertise

JOLT to concat values of the repetitive keys in the same key

New Contributor

Hi there.

 

I would like to concat all the values of a repetitive key and put all of them as the value of one unique key with the same name.

The input would be like:

 

{
    "main": "id;Date;Time;Trigger_time_open;Trigger_time_close;Door;Door_Cycle;Motor_current;Motor_voltage;DLS;DCS;Inclinometer;Temperature"
  },
  {
    "main": "00000;2020/7/18;04:53:37.211;04:53:37.524;04:53:51.847;01;36688;12.062867;0.000000;-1;-1;12.078014;26.303200"
  },
  {
    "main": "00001;;04:53:37.212;;;;;12.064088;0.000000;-1;-1;;"
  }

 

 

An the output something like:

 

{
"main":"id;Date;Time;Trigger_time_open;Trigger_time_close;Door;Door_Cycle;Motor_current;Motor_voltage;DLS;DCS;Inclinometer;Temperature00000;2020/7/18;04:53:37.211;04:53:37.524;04:53:51.847;01;36688;12.062867;0.000000;-1;-1;12.078014;26.30320000001;;04:53:37.212;;;;;12.064088;0.000000;-1;-1;;"
}

 

 

I have been trying with join and concat, but I'm completely new to Jolt transformations, and it's been a pain to find the way.

 

As you can see, the result would be a big string containing all the records of a csv.

 

thank you very much for your help.

1 ACCEPTED SOLUTION

New Contributor

I got the result using:

 

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "main": "main.[&1]"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "main": "=join('\n',@(1,main))"
    }
  }
]

Not sure whether is the best approach. But it works. Thanks a lot

 

View solution in original post

4 REPLIES 4

New Contributor

with this :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "main": "main"
      }
    }
  }
]

 

I am able to get this:

{
  "main" : [ "id;Date;Time;Trigger_time_open;Trigger_time_close;Door;Door_Cycle;Motor_current;Motor_voltage;DLS;DCS;Inclinometer;Temperature", "00000;2020/7/18;04:53:37.211;04:53:37.524;04:53:51.847;01;36688;12.062867;0.000000;-1;-1;12.078014;26.303200", "00001;;04:53:37.212;;;;;12.064088;0.000000;-1;-1;;" ]
}

 

But what I need is a main element with all the values concatenated with a \n among them

New Contributor

I got the result using:

 

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "main": "main.[&1]"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "main": "=join('\n',@(1,main))"
    }
  }
]

Not sure whether is the best approach. But it works. Thanks a lot

 

Expert Contributor

@opalo54 

Thanks for posting your solution!
I was just trying too but I think your input data has to be set to an array for this. Am I getting right?

New Contributor

@justenji 

 

Yes, exactly. All "main" elements are part of an array in the input.

Take a Tour of the Community
Don't have an account?
Your experience may be limited. Sign in to explore more.