You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用jq命令匹配指定cartItemId的JSON对象?

Ah, I see the issue with your jq command! The cartItemId you're looking for isn't at the top level of your JSON object—it's nested inside the fields property of each item entry. Your current command checks the root object for cartItemId, which doesn't exist, hence no results.

Here's how to fix it:

We need to first convert the top-level object into an array of key-value pairs, filter the pairs where the item's fields.cartItemId matches your target, then convert back to an object.

The corrected jq command is:

jq 'to_entries | map(select(.value.fields.cartItemId == 2081021134)) | from_entries' input.json

Let me break down what each part does:

  • to_entries: Turns your top-level object (like {"item_i7bfe8f00": {...}, "item_i7c09dcce": {...}}) into an array of objects like [{"key": "item_i7bfe8f00", "value": {...}}, ...]
  • map(select(.value.fields.cartItemId == 2081021134)): Filters this array to keep only entries where the nested cartItemId matches your desired value
  • from_entries: Converts the filtered array back into a single object with just the matching item

Running this command will give you exactly the expected result:

{
  "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"
  }
}

内容的提问来源于stack exchange,提问作者Joe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:36:17