Hi!
To call data/options in a script step, use this way: @data.mydata@ instead of ${data}, that’s doesn’t work even on command steps, in command steps the correct way to call a data/options is ${data.mydata}. Take a look.
I made a working example using the JSONTest website to retrieve the JSON content and the HTTP Step plugin:
- defaultTab: nodes
description: ''
executionEnabled: true
id: 8c131821-ff27-4c36-8d3b-dd932d35ad7b
loglevel: INFO
name: JQExample
nodeFilterEditable: false
plugins:
ExecutionLifecycle: null
scheduleEnabled: true
sequence:
commands:
- description: Just a friendly message :)
exec: echo "Starting..."
- configuration:
authentication: None
checkResponseCode: 'false'
method: GET
printResponse: 'true'
printResponseToFile: 'false'
proxySettings: 'false'
remoteUrl: http://echo.jsontest.com/key/value/one/two
sslVerify: 'false'
timeout: '30000'
description: Get json data and jq filtering
nodeStep: true
plugins:
LogFilter:
- config:
filter: .
logData: 'true'
prefix: result
type: json-mapper
type: edu.ohio.ais.rundeck.HttpWorkflowNodeStepPlugin
- description: Command step way
exec: 'echo "result is: ${data.one} and ${data.key}"'
- description: Inline script way
fileExtension: .sh
interpreterArgsQuoted: false
script: 'echo "result is: @data.one@ and @data.key@"'
scriptInterpreter: /bin/bash
keepgoing: false
strategy: node-first
uuid: 8c131821-ff27-4c36-8d3b-dd932d35ad7b
Check the result here.
Hope it helps!
Hi,
That is a good scenario, and testing carefully the JQ JSON filter plugin, it seems that in every array iteration the plugin always gets the last entry because the prefix name is always the same. You can open a new enhancement request here.
For that reason, we need a way to iterate the array and put the values with different keys to generate the data values
I created this script that needs jq.
# here you can get the json content using curl.
my_array=( $(echo "[ { \"name\": \"Test\", \"surname\": \"kumar\" }, { \"name\": \"best\", \"surname\": \"yadav\" } ]" | jq -r .[].surname) )
# prints each element as a key/value
for key in ${!my_array[@]}; do
echo "mykey_${key}="${my_array[${key}]}
done
I printed the JSON file directly. You can get the JSON content using curl, then proceed to save the elements in the array, and finally generate the key/value elements.
- defaultTab: nodes
description: ''
executionEnabled: true
id: 8791e56d-b82e-4a80-8efd-64f2856dc299
loglevel: INFO
name: JQExampleArray
nodeFilterEditable: false
plugins:
ExecutionLifecycle: null
scheduleEnabled: true
sequence:
commands:
- description: Just a friendly message :)
exec: echo "Starting..."
- description: Get the JSON data and generate the key/value data
fileExtension: .sh
interpreterArgsQuoted: false
plugins:
LogFilter:
- config:
invalidKeyPattern: \s|\$|\{|\}|\\
logData: 'true'
regex: (.*)=(.*)
type: key-value-data
script: |+
# here you can get the json content using curl.
my_array=( $(echo "[ { \"name\": \"Test\", \"surname\": \"kumar\" }, { \"name\": \"best\", \"surname\": \"yadav\" } ]" | jq -r .[].surname) )
# prints each element as a key/value
for key in ${!my_array[@]}; do
echo "mykey_${key}="${my_array[${key}]}
done
scriptInterpreter: /bin/bash
- description: print the data variable on command step
exec: echo ${data.mykey_0}
- description: print the data variable on inline-script step
fileExtension: .sh
interpreterArgsQuoted: false
script: echo @data.mykey_1@
scriptInterpreter: /bin/bash
keepgoing: false
strategy: node-first
uuid: 8791e56d-b82e-4a80-8efd-64f2856dc299
Check the final result.
Of course, that script is improvable, exists many ways to do the same there :-)
Hope it helps!
Hi Kumar,
@data.result@ is only for inline script steps, if you wanna use the data on the command step, or use a plugin like the HTTP Step please use the ${data.result}.
More info here.
Greetings.