cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Transforming JSON Output to Payload for Slack App

swright
New Contributor III

I have a pipeline that outputs a json file that I would like to reformat into a payload for a Slack webhook app. Itโ€™s for an outlist and the json output list a personโ€™s fullName with startDate and endDate and duration for the absence. The payload would have a header and footer that the data needs to be between.

{
โ€œabsenceTypeโ€: โ€œVacationโ€,
โ€œendDateโ€: โ€œFri, 19 Mayโ€,
โ€œstartDateโ€: โ€œFri, 19 Mayโ€,
โ€œfullNameโ€: โ€œKimberly Smithโ€,
โ€œdurationโ€: 7
},
{
โ€œabsenceTypeโ€: โ€œVacationโ€,
โ€œendDateโ€: โ€œMon, 22 Mayโ€,
โ€œstartDateโ€: โ€œTue, 23 Mayโ€,
โ€œfullNameโ€: โ€œKimberly Smithโ€,
โ€œdurationโ€: 14
},
{
โ€œabsenceTypeโ€: โ€œVacationโ€,
โ€œendDateโ€: โ€œFri, 26 Mayโ€,
โ€œstartDateโ€: โ€œFri, 26 Mayโ€,
โ€œfullNameโ€: โ€œKimberly Smithโ€,
โ€œdurationโ€: 7
},

I want data like the above to be transformed into a payload to look like the following which includes the header and footer that I mentioned. Please note how multiple day absences have different text than single day absences and although this sample data only shows one person one persons absences, there will be multiple people:

{
โ€œchannelโ€: โ€œ#snl-out-listโ€,
โ€œblocksโ€: [
{
โ€œtypeโ€: โ€œsectionโ€,
โ€œtextโ€: {
โ€œtypeโ€: โ€œmrkdwnโ€,
โ€œtextโ€: โ€œAbsences within the next 7 days:โ€
}
},
{
โ€œtypeโ€: โ€œsectionโ€,
โ€œtextโ€: {
โ€œtypeโ€: โ€œmrkdwnโ€,
โ€œtextโ€: โ€œ :palm_tree: Kimberly Smith*โ€
}
},
{
โ€œtypeโ€: โ€œsectionโ€,
โ€œfieldsโ€: [
{
โ€œtypeโ€: โ€œmrkdwnโ€,
โ€œtextโ€: โ€œโ€ข Fri, 19 May:7 hours:Vacationโ€
}
]
},
{
โ€œtypeโ€: โ€œsectionโ€,
โ€œfieldsโ€: [
{
โ€œtypeโ€: โ€œmrkdwnโ€,
โ€œtextโ€: โ€œโ€ข Mon, 22 May to Tues, 23 May:14 hours*:Vacationโ€
}
]
}
{
โ€œtypeโ€: โ€œsectionโ€,
โ€œfieldsโ€: [
{
โ€œtypeโ€: โ€œmrkdwnโ€,
โ€œtextโ€: โ€œโ€ข Fri, 26 May:7 hours:Vacationโ€
}
]
}
],
โ€œusernameโ€: โ€œMy Botโ€
}

1 ACCEPTED SOLUTION

Hey @swright

I was able to generate the expected o/p but what to do with the first two elements, I couldโ€™ve hardcoded it but I didnโ€™t cause I think you are getting that data dynamically from somewhere.
Iโ€™ve attached the sample pipeline with the expression in it.

Test_Expression_2023_06_05.slp (5.6 KB)

image

I hope this helps ๐Ÿ™‚

Cheers

View solution in original post

4 REPLIES 4

swright
New Contributor III

I successfully managed to get the pipeline functioning by utilizing FTP snaps and a Unix Snap to execute a Perl script I developed for the transformation process. However, I am still curious about achieving the same outcome within Snaplogic itself. I attempted using Script snaps with Javascript or Python, but encountered numerous errors that I believe were caused by how the Script snaps implement those languages.

It seems that I frequently find myself needing to rely on external tools instead of utilizing Snaplogic for such processing tasks. I would appreciate exploring how this could have been accomplished within Snaplogic, both with and without the use of a Script snap.

I uploaded sample files with what is to be transformed (sample_incomming_data.json) and what it is to be transformed to (sample_output_payload.json).

sample_incomming_data.json (4.3 KB)
sample_output_payload.json (7.2 KB)

For reference I also uploaded the Perl script that I wrote that successfully does the transformation (transform.pl.txt). This uses the Perl JSON module so I had to install it:

sudo yum install perl-JSON

transform.pl.txt (2.7 KB)

Scott

Hey @swright

I was able to generate the expected o/p but what to do with the first two elements, I couldโ€™ve hardcoded it but I didnโ€™t cause I think you are getting that data dynamically from somewhere.
Iโ€™ve attached the sample pipeline with the expression in it.

Test_Expression_2023_06_05.slp (5.6 KB)

image

I hope this helps ๐Ÿ™‚

Cheers

swright
New Contributor III

Hey @Soni37

Thatโ€™s an exceptional solution! It functions flawlessly and exhibits a remarkable elegance in its relative simplicity. I didnโ€™t think it could be done with so little code and so few snaps!

I initially tried utilizing the Group By N snap, but I couldnโ€™t quite grasp how to utilize the output to achieve my desired outcome. Your inclusion of โ€œgroup.mapโ€ in the mapper function was the missing piece that I needed!

Thanks and regards,
Scott

@swright Glad to know it worked ๐Ÿ™‚.

Please mark my answer as a solution.

Cheers,