【问题标题】:How to match a json value with the jq command?如何用 jq 命令匹配一个 json 值?
【发布时间】:2018-05-22 19:21:22
【问题描述】:

我有以下 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

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

【问题讨论】:

    标签: json jq data-extraction


    【解决方案1】:

    你必须深入挖掘。将对象的值提供给select,并检查.fields.cartItemId,而不仅仅是.cartItemId

    jq '.[] | select(.fields.cartItemId == 2081021134)' input.json
    

    【讨论】:

    • 该命令一开始去掉了"item_i7c09dcce":,请注意输出应该是什么样子
    【解决方案2】:

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

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

    【讨论】:

    • 谢谢,fields 一直都在,所以不用if else,我可以用with_entries(select(.value.fields.cartItemId == 2081021134))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    相关资源
    最近更新 更多