OpenWhisk Action wrapping the JQ command-line utility for JSON filtering.
This Action allows you to create sequences that join Actions whose output and input parameters don't match. The JQ utility is used to filter the output from one Action to match the input expected by another.
This Action is available on Docker Hub at the jamesthomas/openwhisk-jq repository.
OpenWhisk can create Actions from Docker Hub images using the command below.
wsk action create --docker jq jamesthomas/openwhisk-jq
This OpenWhisk Action runs the JQ utility, using the JSON parameters that were used to invoke the Action as input. The Action expects the jq parameter to contain the filter string to process the JSON input. This parameter is removed from the JSON before it is passed to the Action.
This example demonstrates invoking the Action with the simplest filter that returns the JSON input without modification.
wsk action invoke --blocking --result jq --param jq '.' --param message "Hello world"
{
"message": "Hello world"
}
This Action will normally be used as part of an Action sequence, to transform the output from one Action to match the input parameters expected by another.
Binding the jq parameter to the Action before creating a sequence means we don't have to pass this parameter during the Action invocation.
wsk action update jq --param jq '...'
wsk action create my_sequence --sequence /ns/action,/ns/jq,/ns/another_action
wsk action invoke --result --blocking my_sequence
For example, given one Action that returns a message...
wsk action invoke sample --blocking --result
{
"payload": "Hello, my name is James"
}
...and another that reverses words in a sentence.
wsk action invoke reverse --blocking --result --param text "Hello my name is James"
{
"reversed": "James is name my Hello"
}
We can create a new Action, which reverses the message, using the JQ Action to map the output parameter (payload) to the expected input parameter (text).
wsk action update jq --param jq ". | {text: .payload}"
wsk action create reverse_sample --sequence /namespace/sample,/namespace/jq,/namespace/reverse
wsk action invoke reverse_sample --blocking --result
{
"reversed": "James is name my Hello,"
}
The index.js
file contains the Node.js script that calls JQ command-line
utility, passing the Action parameters without the jq property to to
stdio
. JSON returned through stdout
is used as the Action result.
The Alpine Linux Node.js image is used to build a custom Docker image, which installs the JQ utility during the Docker build process.
Use the following commands to build a custom JQ Action, push to Dockerhub and then expose as an OpenWhisk Action.
docker build -t openwhisk-jq .
docker tag openwhisk-jq <dockerhub_username>/openwhisk-jq
docker push <dockerhub_username>/openwhisk-jq
wsk action create --docker jq <dockerhub_username>/openwhisk-jq