Skip to main content
opalo54
Gold ⭐️
Dec 14, 2020 2:57 PM UTC
Solved

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

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.

opalo54
opalo54Answer
Gold ⭐️
Dec 14, 2020 4:12 PM UTC

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

 

4 replies

opalo54
opalo54Author
Gold ⭐️
Dec 14, 2020 3:21 PM UTC

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

Translate
opalo54
opalo54AuthorAnswer
Gold ⭐️
Dec 14, 2020 4:12 PM UTC

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

 

Translate
justenji
Gold ⭐️⭐️
Dec 14, 2020 4:21 PM UTC

@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?

Translate