3

I have a output of curl | jq:

{ "ssh_keys": [ { "id": 30482248, "fingerprint": "21:57:08:d8:41:0f:89:57:0b:96:95:64:ff:59:50:69", "public_key": "ssh-rsa AAAA [email protected]", "name": "User's key" }, { "id": 30450573, "fingerprint": "27:0c:2e:34:63:00:13:9d:5c:26:44:15:f0:cf:15:13", "public_key": "ssh-rsa AAAA-VirtualBox", "name": "test1" }, { "id": 30485316, "fingerprint": "28:63:11:6b:a0:c7:ae:e1:e7:98:0b:01:96:c2:f4:87", "public_key": "ssh-rsa AAAA lessons", "name": "rbm-mykey" } ], "links": { "pages": { "last": "https://api.digitalocean.com/v2/account/keys?page=3", "next": "https://api.digitalocean.com/v2/account/keys?page=2" } }, "meta": { "total": 59 } } 

I want to return "public_key" that have value with "User's key" from "name" key, so the result should be

"ssh-rsa AAAA [email protected]" 

I tried the command:

curl -X GET -H "Authorization: Bearer " "https://api.digitalocean.com/v2/account/keys?page=1" | jq '.[] | .[] | .name' 

But recieved only values of "name" key

0

1 Answer 1

2

Using jq to select the public_key entry for the array element of the ssh_keys array that have a name equal to User's key (where string User's key is given by a value imported into the jq variable $queryname on the command line):

jq -r --arg queryname "User's key" '.ssh_keys[] | select(.name == $queryname).public_key' 

The -r makes the value be printed decoded. Without -r, you'll get a JSON-encoded (and quoted) value back.

You would pipe the JSON output of curl through the above command. With the given document as input, this would generate the single line

ssh-rsa AAAA [email protected] 
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.