user3256222
user3256222

Reputation: 87

Get JSON value from URL in BASH

I need to get the "snapshot" value at the top of the file from this url: https://s3.amazonaws.com/Minecraft.Download/versions/versions.json

So I should get a variable that contains "14w08a" when I run the command to parse the json.

Upvotes: 4

Views: 8129

Answers (3)

notacorn
notacorn

Reputation: 4099

$ curl -s "$url" | jq -r .snapshot
14w08a

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246847

best thing to do is use a tool with a JSON parser. For example:

value=$(
  curl -s "$url" |
  ruby -rjson -e 'data = JSON.parse(STDIN.read); puts data["latest"]["snapshot"]'
)

Upvotes: 4

Chris Seymour
Chris Seymour

Reputation: 85805

This will do the trick

$ curl -s "$url" | grep -Pom 1 '"snapshot": "\K[^"]*'
14w08a

Upvotes: 7

Related Questions