How-to Guide: Capturing JSON from stdout and Reusing It in Another CWL Tool¶
This guide demonstrates how to capture a JSON response from the stdout of one CWL tool and reuse it as input for another tool.
We'll focus on two key sections:
- Capturing JSON output:
In [ ]:
Copied!
cwl_file="../cwl-workflows/capture-json-stdout.cwl"
cwl_file="../cwl-workflows/capture-json-stdout.cwl"
In [ ]:
Copied!
cat $cwl_file | yq e .'$graph[1]' - | yq '.. | select(has("stdout")) | {"stdout": .stdout}'
cat $cwl_file | yq e .'$graph[1]' - | yq '.. | select(has("stdout")) | {"stdout": .stdout}'
In [ ]:
Copied!
cat $cwl_file | yq e .'$graph[1]' - | yq '.. | select(has("outputs")) | {"outputs": .outputs}'
cat $cwl_file | yq e .'$graph[1]' - | yq '.. | select(has("outputs")) | {"outputs": .outputs}'
- Reusing captured JSON in another tool:
In [ ]:
Copied!
cat $cwl_file | yq e .'$graph[2]' - | yq '.. | select(has("arguments")) | {"arguments": (.arguments | .[-2:])}'
cat $cwl_file | yq e .'$graph[2]' - | yq '.. | select(has("arguments")) | {"arguments": (.arguments | .[-2:])}'
Objective¶
- Tool 1 (
stac): Fetch a STAC item in JSON format and extract itsassets. - Tool 2 (
rio): Process the extracted asset to generate a PNG image based on the specified band.
In [ ]:
Copied!
cat $cwl_file | yq e .'$graph[1]' - | yq '.. | select(has("stdout")) | {"stdout": .stdout}'
cat $cwl_file | yq e .'$graph[1]' - | yq '.. | select(has("stdout")) | {"stdout": .stdout}'
In [ ]:
Copied!
cat $cwl_file | yq e .'$graph[1]' - | yq '.. | select(has("outputs")) | {"outputs": .outputs}'
cat $cwl_file | yq e .'$graph[1]' - | yq '.. | select(has("outputs")) | {"outputs": .outputs}'
stdout: message: Redirects the tool'sstdoutto a file namedmessage.glob: message: Locates themessagefile.loadContents: true: Reads the content of the file into memory.outputEval: Parses the JSON content to extract theassetsfield using JavaScript (JSON.parse).
How It Works
- The
stactool fetches a STAC item as JSON usingcurl. - The output (
stdout) is saved tomessage. - The
outputsblock processes this JSON and makes theassetsfield available for downstream steps.
- Reusing Captured JSON in Another Tool
In the rio tool:
Relevant Block
In [ ]:
Copied!
cat $cwl_file | yq e .'$graph[2]' - | yq '.. | select(has("arguments")) | {"arguments": (.arguments | .[-2:])}'
cat $cwl_file | yq e .'$graph[2]' - | yq '.. | select(has("arguments")) | {"arguments": (.arguments | .[-2:])}'
- First
valueFrom: Selects the asset whoseeo:bandsarray contains a band with acommon_namematchinginputs.common_band_nameand returns itshref. - Second
valueFrom: Generates the output filename for the PNG (red.pngfor theredband).
How It Works
- The
riotool processes the asset URL (href) extracted in the first step.` - It generates a PNG file using the
common-band-nameand saves it as `.png.
- Complete CWL Example
Here’s the full capture-json-stdout.cwl file:
In [ ]:
Copied!
yq e . $cwl_file
yq e . $cwl_file
It's graphical representation:
In [ ]:
Copied!
cwltool --print-dot $cwl_file 2> /dev/null | dot -Tpng | display
cwltool --print-dot $cwl_file 2> /dev/null | dot -Tpng | display
- Run the Example
Command to execute the workflow:
cwltool capture-json-stdout.cwl \
--stac-item https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items/S2B_53HPA_20210723_0_L2A \
--common-band-name red
- Execute the CWL Tool
Run the command in your terminal:
cwltool capture-json-stdout.cwl \
--stac-item https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items/S2B_53HPA_20210723_0_L2A \
--common-band-name red
In [ ]:
Copied!
cat > input.json <<'JSON'
{
"stac-item": {
"value": "https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items/S2B_53HPA_20210723_0_L2A"
},
"common-band-name": "red"
}
JSON
cwltool --podman "$cwl_file" input.json > output.json
cat > input.json <<'JSON'
{
"stac-item": {
"value": "https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items/S2B_53HPA_20210723_0_L2A"
},
"common-band-name": "red"
}
JSON
cwltool --podman "$cwl_file" input.json > output.json
Expected Output¶
- Intermediate Output (
json_output): Extracted assets from the STAC item JSON. - Final Output (
preview): PNG file for the specified band (e.g.,red.png).
In [ ]:
Copied!
cat output.json | yq e '.json_output' -o=json -
cat output.json | yq e '.json_output' -o=json -
In [ ]:
Copied!
cat output.json | yq e '.preview' -o=json -
cat output.json | yq e '.preview' -o=json -
Key Takeaways¶
Capturing JSON:
- Use
stdoutto redirect the JSON output to a file. - Parse and extract specific fields using
outputEval.
- Use
Reusing JSON:
- Use
valueFromin arguments to select fields dynamically from the JSON input. - Process the selected field (e.g., generate a file based on its href).
- Use
By focusing on stdout and outputBinding, this guide highlights how CWL facilitates JSON data flow between tools.
In [ ]:
Copied!
rm -f output.json
rm -f red.png
rm -f output.json
rm -f red.png