jq语法错误提取名称中带有冒号的键 [英] jq syntax error extracting key with colon in its name
问题描述
我有以下JSON(可以从 https:中检索到): //openlibrary.org/api/books?bibkeys=ISBN:039397281X&format=json )作为输入:
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"
}
}
我的目标是提取preview_url
-因此,对于上面的输入,获得输出https://archive.org/details/isbn_9780393972818
.
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
如何解决?
推荐答案
您想要的是:
jq -r '.["ISBN:039397281X"].preview_url'
或更简单地说:
jq -r '.[].preview_url'
仅当foo
是有效标识符时,语法.foo
只能用于提取与键foo
关联的值.冒号在标识符名称中无效,因此必须使用其他语法(.["foo"]
).
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.
这篇关于jq语法错误提取名称中带有冒号的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!