Reputation: 200
I have the following JSON (which can be retrieved from https://openlibrary.org/api/books?bibkeys=ISBN:039397281X&format=json) as input:
{
"ISBN:039397281X": {
"bib_key": "ISBN:039397281X",
"preview": "borrow",
"thumbnail_url": "https://covers.openlibrary.org/b/id/7890978-S.jpg",
"preview_url": "https://archive.org/details/isbn_9780393972818",
"info_url": "https://openlibrary.org/books/OL348852M/Don_Quijote"
}
}
My goal is to extract the preview_url
-- thus, for the input above, to get the output https://archive.org/details/isbn_9780393972818
.
I'm currently using the following:
curl 'https://openlibrary.org/api/books?bibkeys=ISBN:039397281X&format=json' \
| jq -r '.ISBN:039397281X' | jq -r '.preview_url'
However, this emits an error:
jq: error: syntax error, unexpected ':', expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
.ISBN:039397281X
jq: 1 compile error
How can it be resolved?
Upvotes: 6
Views: 6564
Reputation: 295696
What you want is:
jq -r '.["ISBN:039397281X"].preview_url'
or, more simply:
jq -r '.[].preview_url'
The syntax .foo
can only be used to extract the value associated with key foo
when foo
is a valid identifier. Colons are not valid in identifier names, so a different syntax (.["foo"]
) must be used.
Upvotes: 15