我如何使用JQ过滤具有最近对象键的JSON文件?

时间:2019-08-28 14:36:13

标签: jq

我必须使用JQ过滤具有最接近对象名称的JSON文件。

我已经尝试使用命令:

jq '.data[] |select(.orderid=="40007600")' output.json

,但是该命令仅过滤名称正确的对象。我想过滤以字符串“ 40007”开头的对象,例如:.orderid="40007"

{
  "data": [
    {
      "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"
    },
     {
      "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"
    },
     {
      "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"
    }
]
}

1 个答案:

答案 0 :(得分:1)

jq '.data[] | select(.orderid | startswith("40007"))' output.json