4

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.

user3256222
  • 87
  • 3
  • 7

3 Answers3

7

This will do the trick

$ curl -s "$url" | grep -Pom 1 '"snapshot": "\K[^"]*'
14w08a
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
  • 3
    +1 for this pragmatic approach (in general, @glennjackman's approach offers more flexibility); version for platforms where `-P` is not supported (e.g, OSX): `curl -s "$url" | egrep -m 1 '"snapshot"' | awk -F '"' '{ print $4 }'` – mklement0 Feb 19 '14 at 22:12
  • `-Pom` has three options: [`-P` is for `--perl-regexp`, and `-o` is for `--only-matching`, and `-m` is for `--max-count`](https://man7.org/linux/man-pages/man1/grep.1.html). And p[`\K` is `perl-regexp` flavor for a variable-length lookbehind](https://stackoverflow.com/a/33573989/1175496), "`\K` tells the engine to pretend that the match attempt started at this position."; another [example shown here](https://stackoverflow.com/a/5081519/1175496) – Nate Anderson Dec 28 '22 at 14:02
4

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"]'
)
mklement0
  • 382,024
  • 64
  • 607
  • 775
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0
$ curl -s "$url" | jq -r .snapshot
14w08a
notacorn
  • 3,526
  • 4
  • 30
  • 60