How to stream a binary PDF file to a file using Snaplogic
I am trying to stream a binary PDF file to a file using Snaplogic.
The equivalent would be to the following command:
curl “http://www.pdf995.com/samples/pdf.pdf” --output …/out/pdf.pdf
Is this ...
Since the CSV Formatter currently doesn’t work with arrays, you will need to use a child pipeline that splits the array into separate docs that go into a CSV Formatter.
I’m attaching a couple of pipelines to demonstrate this flow:
The FormatArrayToCSV pipeline is the child. It splits the “$array” property in the incoming document and then passes the results to a CSV Formatter and FileWriter. You’ll need to change the JSON Splitter to split your array property.
The WriteCSVFiles pipeline is the parent. It just contains some mock data and feeds it into the child pipeline.
Coyote - this is actually a fairly tricky question. Since the SnapLogic expression language doesn't allow for multi-line syntax, the only solution without reverting to a Script snap would be to create an expression that can call itself recursively. This is where expression libraries will come in to help us. I apologize that I don't have a lot of time to completely detail out my answer, but will give you what I can in just a few minutes.
First, the solution. Please find the sample pipeline and expression library file attached here. Just download it, then upload the expr lib file to your project, then import the SLP with the example pipeline. You'll see your requested object as well as an additional array to test nested arrays containing objects. Basically I just wanted to test a more complex example to ensure I had that working correctly.
The call to the expr lib function is very simple:
I'm just putting the results back to the root object in the target path ($).
But let's take a look in the lib expr file itself to see how it's doing this:
lowerCaseKeys : (obj) =>
// Recursively convert all object keys to lowercase
obj
.mapValues((val,key) =>
match typeof val {
'object' => this.lowerCaseKeys (val)
, 'array' => val.map(elem => typeof elem == 'object' ? this.lowerCaseKeys(elem) : elem)
, _ => val
}
)
.mapKeys((val,key) => key.toLowerCase())
I'm first calling the Object.mapValues() method to check for any sub-elements that are objects or array of objects - in either case, I'm going to recursively call the lowerCaseKeys() function to do that next lower level of the nested object, leaving all the current values as-is.
After we're done with the mapValues() call, then I can do the rest of the keys at the root level using the Object.mapKeys() method.
I also included an upperCaseKeys() function in the expr file for completeness. There are other functions in the expr file that would take much longer to explain, but are just as useful as what I've explained here.
Documents in snaplogic are always in JSON when coming out of a circle connector (as opposed to the diamond connectors). In this case the XML string that is generated is put on an xml field in the generated json document, so if you want to send that to a REST call you would have to properly reference that field.