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,