Pipe in JSON or take "variable" into JSON file?

Not sure how to word this but....

I want to use CLI to do something lke a GDPR delete request. The JSON for the delete API needs the subject passed in like the userId. I'm hoping to be able to do something like a gc users list with a select/where clause and retrieve just the userIDs for users (from a specific division or group). I want to then be able to loop or do a for-each on each of those IDs. The problem is how can I pass that ID value through to the subsequent query?

Other that piping in a pre-set JSON file, can I pipe the JSON syntax through in-line somehow? Anybody got an example of how that might look?

Hi,

To pipe inline without a JSON file you could use a command such as the following:

echo '{"name": "test cli user", "email": "test_cli_user@test.com"}' | gc users create

Hi,

To add to Ronan's answer, I'm not sure how you could achieve this with a single CLI command, but this bash script example could help:

gc users list | jq '[.entities | .[] | select(.division.id=="<division ID>") | .id]' | while read object; do 
    if [ "${object}" != "[" ] && [ "${object}" != "]" ]; then         
        ID=$(echo $object | sed 's/"//g')
        ID=$(echo $ID | sed 's/,//g')
        gc users get $ID
    fi
done

If I'm not mistaken, your question is how could you treat the resulting JSON array as a bash array so you can use the items for more commands. The solution I found above is a bit messy but it works.

Regards

Thanks Ronan, and Charlie

Unless I've missed it, this would be handy to have in the CLI documentation.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.