如何将json值与jq命令匹配?

时间:2018-05-22 19:21:23

标签: json jq data-extraction

我有以下json数据:

{
    "item_i7bfe8f00": {
        "id": "i7bfe8f00",
        "tag": "item",
        "fields": {
            "img": "https://example.com",
            "quantity": {
                "qtyPrefix": "Kuantitas",
                "min": 1,
                "autoOptions": false,
                "quantity": 5,
                "max": 5,
                "editable": true,
                "showIncrDecr": true,
                "showOptions": false,
                "step": 1
            },
            "sellerName": "ALL ITEM STORE",
            "title": "Xiaomi Redmi 4A Softcase Black",
            "stockTip": {},
            "valid": true,
            "itemId": "143800088",
            "operations": [
                "wishlist",
                "delete"
            ],
            "sellerId": "100124080",
            "price": {
                "price": 3500,
                "currentPrice": "Rp3.500",
                "originPrice": "Rp15.000",
                "promotionRatio": "-77%"
            },
            "restriction": false,
            "isGift": false,
            "sku": {
                "skuText": "Softcase, Hitam",
                "productVariant": "SO908ELAAVYY4AANID-72544754",
                "brandId": "17818",
                "skuId": "157608391"
            },
            "itemUrl": "https://example.com",
            "cartItemId": 2080280320
        },
        "type": "biz"
    },
    "item_i7c09dcce": {
        "id": "i7c09dcce",
        "tag": "item",
        "fields": {
            "img": "https://example.com",
            "quantity": {
                "qtyPrefix": "Kuantitas",
                "min": 1,
                "autoOptions": false,
                "quantity": 1,
                "max": 5,
                "editable": true,
                "showIncrDecr": true,
                "showOptions": false,
                "step": 1
            },
            "sellerName": "PT. Ecart Services Indonesia",
            "title": "Black",
            "stockTip": {},
            "valid": true,
            "itemId": "345695828",
            "operations": [
                "wishlist",
                "delete"
            ],
            "sellerId": "100161156",
            "price": {
                "price": 2499000,
                "currentPrice": "Rp2.499.000"
            },
            "restriction": false,
            "isGift": false,
            "sku": {
                "skuText": "Hitam",
                "productVariant": "345695828_ID-359330058",
                "brandId": "21734",
                "skuId": "359330058"
            },
            "itemUrl": "https://example.com",
            "cartItemId": 2081021134
        },
        "type": "biz"
    }
}

我希望显示只有"cartItemId": 2081021134的对象,因此最终结果应如下所示:

{
    "item_i7c09dcce": {
        "id": "i7c09dcce",
        "tag": "item",
        "fields": {
            "img": "https://example.com",
            "quantity": {
                "qtyPrefix": "Kuantitas",
                "min": 1,
                "autoOptions": false,
                "quantity": 1,
                "max": 5,
                "editable": true,
                "showIncrDecr": true,
                "showOptions": false,
                "step": 1
            },
            "sellerName": "PT. Ecart Services Indonesia",
            "title": "Black",
            "stockTip": {},
            "valid": true,
            "itemId": "345695828",
            "operations": [
                "wishlist",
                "delete"
            ],
            "sellerId": "100161156",
            "price": {
                "price": 2499000,
                "currentPrice": "Rp2.499.000"
            },
            "restriction": false,
            "isGift": false,
            "sku": {
                "skuText": "Hitam",
                "productVariant": "345695828_ID-359330058",
                "brandId": "21734",
                "skuId": "359330058"
            },
            "itemUrl": "https://example.com",
            "cartItemId": 2081021134
        },
        "type": "biz"
    }
}

这是我到目前为止所尝试的:

jq 'select(.cartItemId==2081021134)' input.json

但它什么也没有返回,我该如何解决?

2 个答案:

答案 0 :(得分:3)

你必须深入挖掘一下。将对象的值提供给keyValue,然后选中select,而不只是.fields.cartItemId

.cartItemId

答案 1 :(得分:1)

这会产生所需的输出。使用with_entries可以轻松保留密钥。

with_entries( if .value | has("fields") 
              then select(.value.fields.cartItemId == 2081021134)
              else . end)