Как сопоставить значение json с командой jq? - PullRequest
0 голосов
/ 22 мая 2018

У меня есть следующие данные 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 голосов
/ 22 мая 2018

Это дает желаемый результат.Использование with_entries упрощает сохранение ключей.

with_entries( if .value | has("fields") 
              then select(.value.fields.cartItemId == 2081021134)
              else . end)
0 голосов
/ 22 мая 2018

Вы должны копать немного глубже.Введите значения объекта в select и проверьте .fields.cartItemId, а не просто .cartItemId.

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