我有以下JSON文件:
{
"error": 0,
"data": {
"0": {
"orderid": "40007600",
"price": "9.99",
"listingname": "iPhone 8",
"smallphoto": "https://images-eu.ssl-images-amazon.com/images/I/51b5iaLTjgL._SL160_.jpg",
"photo": "https://images-eu.ssl-images-amazon.com/images/I/51b5iaLTjgL.jpg"
},
"1": {
"orderid": "40007598",
"price": "9.99",
"listingname": "iPhone 8 Plus",
"smallphoto": "https://images-eu.ssl-images-amazon.com/images/I/51CVLqsSNkL._SL160_.jpg",
"photo": "https://images-eu.ssl-images-amazon.com/images/I/51CVLqsSNkL.jpg"
},
"4": {
"orderid": "40007595",
"price": "9.39",
"listingname": "Nadelflaschen",
"smallphoto": "https://images-eu.ssl-images-amazon.com/images/I/41OHDP2rAeL._SL160_.jpg",
"photo": "https://images-eu.ssl-images-amazon.com/images/I/41OHDP2rAeL.jpg"
},
}
}
如何使用bash脚本过滤所有内容,以便获得" smallphoto"的所有图像网址。进入文本文件?
f.e。来自" Nadelflaschen"
的https://images-eu.ssl-images-amazon.com/images/I/41OHDP2rAeL.SL160.jpg我尝试了awk和jq命令。但我只是要转储以了解jq语法。
awk是可能的,但是我只使用反斜杠的未过滤链接,这不起作用。
答案 0 :(得分:3)
jq '.data[] | .smallphoto' input.json
的产率:
"https://images-eu.ssl-images-amazon.com/images/I/51b5iaLTjgL._SL160_.jpg"
"https://images-eu.ssl-images-amazon.com/images/I/51CVLqsSNkL._SL160_.jpg"
"https://images-eu.ssl-images-amazon.com/images/I/41OHDP2rAeL._SL160_.jpg"
这里的关键点是.[]
(.data[]
是.data|.[]
的缩写形式)可以与JSON对象一起使用,而不仅仅是数组。
答案 1 :(得分:1)
这是另一个过滤器,无论嵌套有多深,都会找到.smallphoto
:
.. | .smallphoto?//empty
样本运行(假设data.json
中的更正样本数据)
$ jq -M '.. | .smallphoto?//empty' data.json
"https://images-eu.ssl-images-amazon.com/images/I/51b5iaLTjgL._SL160_.jpg"
"https://images-eu.ssl-images-amazon.com/images/I/51CVLqsSNkL._SL160_.jpg"
"https://images-eu.ssl-images-amazon.com/images/I/41OHDP2rAeL._SL160_.jpg"