Tutorial 07: Load a JSON file into a pipeline
Introduction
This Tutorial is targeting
Automation Experts who want to learn how to create a pipeline which can access any data using the Pipeline Expression Language (PEL). This is handy, in case you want to analyze or transform data, which is in most cases stored in the body of the pipeline.
What you will learn
The question to solve in this tutorial is, how you can load an external JSON file into the pipeline body in a way that you can also apply a PE on it.
Step 1: Create a JSON file in Drive
Login to the portal https://NAMESPACE.pipeforce.net
Navigate to
Files
. The Drive app opens.Add a new text document
person.json
in the root folder, and copy paste this content into it:
{
"firstName":"Marissa",
"lastName":"Smith",
"age":33
}
Step 2: Load the JSON file from Drive
Navigate to
Workbench
. The ad-hoc pipeline editor opens.Replace the existing content by this pipeline:
pipeline:
- drive.read:
path:"person.json"
RUN the pipeline, and you should see the content of the file
person.json
as output:
{
"firstName":"Marissa",
"lastName":"Smith",
"age":33
}
This is because the file is automatically converted from a content object to its the mime type format, which is JSON in this case, and then sent to the client.
In order to “see” the content object and not the result, change the pipeline to this:
pipeline:
- drive.read:
path:"person.json"
- set.body:
value:"#{@json.stringify(body)}"
RUN the pipeline, and you should see as output, the content object with meta information about the file:
{
"name":"person.json",
"created":1637045518731,
"lastUpdated":null,
"size":64,
"data":"ewogICJmaXJzdE5hbWUiOiAiTWFyaXNzYSIs...",
"mimeType":"application/json"
}
Step 3: Convert the content data to JSON
In case you want to work with the JSON data of a file, the data part of the content object is the interesting thing for you. In order to load this into the body, again use the @convert.toJson()
, but this time, point to the data property:
pipeline:
- drive.read:
path:"person.json"
-set.body:
value:"#{@convert.toJson(body.data)}"
After you RUN the pipeline, you should see again the JSON content as output:
{
"firstName":"Marissa",
"lastName":"Smith",
"age":33
}
Congratulations!