Bash + curl + jq: 7373 66 bytes
Shortest answer that doesn't use an xkcd-specific library. jq is a tool for manipulating json objects in the shell, and it comes complete with a parsing language to do that.
curl -Ls xkcd.com/$1/info.0.json|jq -r 'if.num==859then.num.a else.alt end'
curl -Ls xkcd.com/$1/info.0.json|jq -r 'if.num==859then'(.num!=859//.a else[9]|not)//.alt end'alt'
Expansion below:
curl -Ls
- Query, but feel free to redirect (in this case to the https site) and give no unrelated output.
xkcd.com/$1/info.0.json
- Shamelessly stolen from another answer.
|jq -r
- Run jq
in "raw output" mode on the following command.
.num == 859
then
.num.a # This fails because you can't get the key 'a' from a property that's an integer
else
.alt # And this pulls out the 'alt' key from our object.
end
Now the script has been re-worked to use //
which is the equivalent of a or b
in python, and we use a |not
to make any true value be considered false, so the second //
can print .alt