代码之家  ›  专栏  ›  技术社区  ›  Joe

如何使用jq获取包含特定字符串的json密钥?

  •  0
  • Joe  · 技术社区  · 7 年前

    我有这样的json数据:

    {
      "success": true,
      "module": {
        "endpoint": {
          "mode": "pc",
          "protocolVersion": "2.0"
        },
        "reload": true,
        "data": {
          "leftContainer_CL": {
            "id": "CL",
            "tag": "leftContainer",
            "fields": {
              "css": {
                "floatPosition": "left",
                "width": "788px"
              },
              "tag": "leftContainer"
            },
            "type": "container"
          },
          "container_C": {
            "id": "C",
            "tag": "container",
            "fields": {
              "css": {
                "marginTop": "12px"
              },
              "tag": "container"
            },
            "type": "container"
          },
          "delivery_dfdaf8a8a": {
            "id": "dfdaf8a8a",
            "tag": "delivery",
            "fields": {
              "selectPos": "right",
              "deliveryBy": {
                "text": "Disediakan oleh",
                "poster": "ALL ITEM STORE"
              },
              "options": [
                {
                  "highlight": false,
                  "deliveryId": "STANDARD",
                  "bgColor": "#fafafa",
                  "price": "Rp18.900",
                  "disable": false,
                  "reachTime": "Dapatkan pada\n 3-4 Apr 2018",
                  "liveUp": false,
                  "selected": true
                }
              ],
              "style": "bar"
            },
            "type": "biz"
          },
          "rightContainer_CR": {
            "id": "CR",
            "tag": "rightContainer",
            "fields": {
              "css": {
                "floatPosition": "right",
                "width": "388px"
              },
              "tag": "rightContainer"
            },
            "type": "container"
          },
          "delivery_d43597338a": {
            "id": "d43597338a",
            "tag": "delivery",
            "fields": {
              "selectPos": "right",
              "deliveryBy": {
                "text": "Disediakan oleh",
                "poster": "incredible accessories hp"
              },
              "options": [
                {
                  "highlight": false,
                  "deliveryId": "STANDARD",
                  "bgColor": "#fafafa",
                  "price": "Rp37.800",
                  "disable": false,
                  "reachTime": "Dapatkan pada\n 3-4 Apr 2018",
                  "liveUp": false,
                  "selected": true
                }
              ],
              "style": "bar"
            },
            "type": "biz"
          },
          "orderSummary_6": {
            "id": "6",
            "tag": "orderSummary",
            "fields": {
              "isOpen": "false",
              "summarys": [
                {
                  "tail": "(3 barang)",
                  "title": "Subtotal",
                  "value": "Rp23.557"
                },
                {
                  "title": "Biaya pengiriman",
                  "value": "Rp56.700"
                }
              ],
              "title": "Ringkasan Pesanan\r\n"
            },
            "type": "biz"
          },
          "root_0": {
            "id": "0",
            "tag": "root",
            "fields": {
              "count": 3,
              "title": "Troli belanja Saya"
            },
            "type": "root"
          },
          "item_i77997d6b": {
            "id": "i77997d6b",
            "tag": "item",
            "fields": {
              "img": "https://id-live.slatic.net/original/08c1396908dc240625751b09decb4211.jpg",
              "quantity": {
                "qtyPrefix": "Kuantitas",
                "min": 1,
                "autoOptions": false,
                "quantity": 1,
                "max": 5,
                "editable": true,
                "showIncrDecr": true,
                "showOptions": false,
                "step": 1
              },
              "sellerName": "ALL ITEM STORE",
              "title": "Case Slim Black Matte Xiaomi Redmi 4A Softcase Black",
              "stockTip": {},
              "valid": true,
              "itemId": "143800088",
              "operations": [
                "wishlist",
                "delete"
              ],
              "sellerId": "100124080",
              "price": {
                "price": 6000,
                "currentPrice": "Rp6.000",
                "originPrice": "Rp30.000",
                "promotionRatio": "-80%"
              },
              "restriction": false,
              "isGift": false,
              "sku": {
                "skuText": "Softcase, Hitam",
                "productVariant": "SO908ELAAVYY4AANID-72544754",
                "brandId": "17818",
                "skuId": "157608391"
              },
              "itemUrl": "https://www.lazada.co.id/products/i143800088-s157608391.html?urlFlag=true&mp=1",
              "cartItemId": 2006547819
            },
            "type": "biz"
          },
          "location_2": {
            "id": "2",
            "tag": "location",
            "fields": {
              "buttonText": "GANTI\r\n",
              "editable": true,
              "postCode": "",
              "style": "casAddress",
              "label": "Lokasi",
              "title": "Jawa Tengah,Kab. Boyolali,Ampel",
              "addressId": "R2388357-R80010396-R80015219"
            },
            "type": "biz"
          },
          "voucherInput_7": {
            "id": "7",
            "tag": "voucherInput",
            "fields": {
              "buttonText": "GUNAKAN",
              "placeHolder": "Masukkan Kode Voucher",
              "status": "default"
            },
            "type": "biz",
            "validate": {
              "value": [
                {
                  "msg": "Maaf, voucher ini tidak dapat digunakan. Silahkan periksa jika ada kesalahan penulisan",
                  "regex": "^$|^[ ]{0,5}[A-Za-z0-9~!@#%&*()_+?<>{}|-]{1,100}[ ]{0,5}$"
                }
              ]
            }
          },
          "shop_43597338a_s2c": {
            "id": "43597338a_s2c",
            "tag": "shop",
            "fields": {
              "badges": [],
              "link": "//www.lazada.co.id/shop/incredible-accessories-hp",
              "name": "incredible accessories hp"
            },
            "type": "biz"
          },
          "item_i7799f86e": {
            "id": "i7799f86e",
            "tag": "item",
            "fields": {
              "img": "http://id-live-02.slatic.net/p/2/case-anti-shock-anti-crack-elegant-softcase-for-xiaomi-redmi-5a-white-clear-free-tempered-glass-1273-94487227-9f8ddff53bde3f8de9eb514ba2172361-catalog.jpg",
              "quantity": {
                "qtyPrefix": "Kuantitas",
                "min": 1,
                "autoOptions": false,
                "quantity": 1,
                "max": 5,
                "editable": true,
                "showIncrDecr": true,
                "showOptions": false,
                "step": 1
              },
              "sellerName": "incredible accessories hp",
              "title": "Case Anti Shock / Anti Crack Elegant Softcase  for Xiaomi Redmi 5A - White Clear + Free Tempered Glass",
              "stockTip": {},
              "valid": true,
              "itemId": "160714927",
              "operations": [
                "wishlist",
                "delete"
              ],
              "sellerId": "53631",
              "price": {
                "price": 13580,
                "currentPrice": "Rp13.580",
                "originPrice": "Rp25.000",
                "promotionRatio": "-46%"
              },
              "restriction": false,
              "isGift": false,
              "sku": {
                "skuText": "Softcase, Bening",
                "productVariant": "SO908ELAB716EPANID-97510528",
                "brandId": "17818",
                "skuId": "183461134"
              },
              "itemUrl": "https://www.lazada.co.id/products/i160714927-s183461134.html?urlFlag=true&mp=1",
              "cartItemId": 2006579310
            },
            "type": "biz"
          },
          "package_p43597338a": {
            "id": "p43597338a",
            "tag": "package",
            "fields": {},
            "type": "biz"
          },
          "listHeader_H": {
            "id": "H",
            "tag": "listHeader",
            "fields": {
              "middle": "HARGA",
              "left": "3 barang",
              "right": "KUANTITAS"
            },
            "type": "biz"
          },
          "delivery_3": {
            "id": "3",
            "tag": "delivery",
            "fields": {
              "selectPos": "left",
              "options": [
                {
                  "highlight": false,
                  "deliveryId": "STANDARD",
                  "bgColor": "#fafafa",
                  "price": "Rp56.700",
                  "disable": false,
                  "icon": "https://laz-img-cdn.alicdn.com/tfs/TB1UpyCpfDH8KJjy1XcXXcpdXXa-72-72.png",
                  "name": "Standar",
                  "reachTime": "Dapatkan pada\n 3-4 Apr 2018",
                  "liveUp": false,
                  "selected": true
                }
              ],
              "style": "card",
              "title": "Pengiriman yang dipilih"
            },
            "type": "biz"
          },
          "orderTotal_8": {
            "id": "8",
            "tag": "orderTotal",
            "fields": {
              "button": {
                "enable": true,
                "text": "LANJUTKAN KE PEMBAYARAN",
                "clicked": false
              },
              "payment": {
                "taxTip": "Termasuk PPN, jika berlaku",
                "pay": "Rp80.257",
                "title": "Total"
              }
            },
            "type": "biz"
          },
          "shop_fdaf8a8a_s23f9": {
            "id": "fdaf8a8a_s23f9",
            "tag": "shop",
            "fields": {
              "badges": [],
              "link": "//www.lazada.co.id/shop/all-item-store",
              "name": "ALL ITEM STORE"
            },
            "type": "biz"
          },
          "floatTips_4": {
            "id": "4",
            "tag": "floatTips",
            "fields": {
              "tips": []
            },
            "type": "biz"
          },
          "item_i7790e0f9": {
            "id": "i7790e0f9",
            "tag": "item",
            "fields": {
              "img": "http://id-live-02.slatic.net/p/2/case-anti-shock-anti-crack-elegant-softcase-for-xiaomi-redmi-note4x-white-clear-8431-85175402-0bec01e88741744ae5461c4b3a4ae160-catalog.jpg",
              "quantity": {
                "qtyPrefix": "Kuantitas",
                "min": 1,
                "autoOptions": false,
                "quantity": 1,
                "max": 5,
                "editable": true,
                "showIncrDecr": true,
                "showOptions": false,
                "step": 1
              },
              "sellerName": "incredible accessories hp",
              "title": "Case Anti Shock / Anti Crack Elegant Softcase  for Xiaomi Redmi Note 4x - White Clear",
              "stockTip": {},
              "valid": true,
              "itemId": "108849535",
              "operations": [
                "wishlist",
                "delete"
              ],
              "sellerId": "53631",
              "price": {
                "price": 3977,
                "currentPrice": "Rp3.977",
                "originPrice": "Rp15.000",
                "promotionRatio": "-73%"
              },
              "restriction": false,
              "isGift": false,
              "sku": {
                "skuText": "Sarung, Bening",
                "productVariant": "CA529ELAAC6GUEANID-27304198",
                "brandId": "10464",
                "skuId": "110628148"
              },
              "itemUrl": "https://www.lazada.co.id/products/i108849535-s110628148.html?urlFlag=true&mp=1",
              "cartItemId": 2005983481
            },
            "type": "biz"
          },
          "package_pfdaf8a8a": {
            "id": "pfdaf8a8a",
            "tag": "package",
            "fields": {},
            "type": "biz"
          }
        },
        "hierarchy": {
          "component": [
            "container",
            "delivery",
            "item",
            "shop",
            "package",
            "listHeader",
            "orderSummary",
            "leftContainer",
            "orderTotal",
            "floatTips",
            "root",
            "location",
            "rightContainer",
            "voucherInput"
          ],
          "root": "root_0",
          "structure": {
            "package_p43597338a": [
              "delivery_d43597338a",
              "shop_43597338a_s2c",
              "item_i7799f86e",
              "item_i7790e0f9"
            ],
            "leftContainer_CL": [
              "delivery_3",
              "listHeader_H",
              "package_pfdaf8a8a",
              "package_p43597338a"
            ],
            "container_C": [
              "leftContainer_CL",
              "rightContainer_CR"
            ],
            "rightContainer_CR": [
              "location_2",
              "orderSummary_6"
            ],
            "orderSummary_6": [
              "voucherInput_7",
              "orderTotal_8"
            ],
            "root_0": [
              "container_C",
              "floatTips_4"
            ],
            "package_pfdaf8a8a": [
              "delivery_dfdaf8a8a",
              "shop_fdaf8a8a_s23f9",
              "item_i77997d6b"
            ]
          }
        },
        "linkage": {
          "input": [],
          "request": [
            "voucherInput_7",
            "item_i7799f86e",
            "delivery_3",
            "orderTotal_8",
            "item_i7790e0f9",
            "location_2",
            "item_i77997d6b"
          ],
          "common": {
            "compress": true,
            "queryParams": "^^$$1afe141216814f45e9fc6dba84d4863d{$_$}H4sIAAAAAAAAAFWU247bRgyGXyXQ9caYGXJOe5fkJmmLot3de4Ez5OwKlSVBklMkQd69lJsNHBgwZIHfz9NPf+t2Wc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fS2uwx7UoumKUNoPakK2qeY0ko10EIVR5w5wBqNhTI0bhXhv/kk3aM93NORr4MgcH9OdomiLPhiB9+YtexRuEuzkrnCoM5bf2tqr7vQGD8eVpdVjwAAAA=="
          },
          "signature": "89dffd9ca23307e603556a4c896e4c56"
        }
      }
    }
    

    具有 jq 我可以这样做: [me@linux]$ cat /tmp/json | jq '.module | .data | keys' [ "container_C", "delivery_3", "delivery_d43597338a", "delivery_dfdaf8a8a", "floatTips_4", "item_i7790e0f9", "item_i77997d6b", "item_i7799f86e", "leftContainer_CL", "listHeader_H", "location_2", "orderSummary_6", "orderTotal_8", "package_p43597338a", "package_pfdaf8a8a", "rightContainer_CR", "root_0", "shop_43597338a_s2c", "shop_fdaf8a8a_s23f9", "voucherInput_7" ] 我需要 orderTotal_8 但请注意 8 总是在变化,所以它可以 orderTotal_10 orderTotal_3 等我怎样才能用pure拿到这把钥匙 jq公司 如果可能,在没有grep/awk帮助的情况下进行命令?所以结果应该是这样的 orderTotal\u 8

    1 回复  |  直到 7 年前
        1
  •  0
  •   peak    7 年前

    如果要形成满足某些条件的项的子数组,只需将map(选择(条件))添加到管道中,例如:。

    .module | .data | keys | map(select(test("^orderTotal_")))
    

    如果您只需要一个与条件匹配的项目流,则可以首先形成该流,然后进行选择:

    .module | .data | keys[] | select(test("^orderTotal_"))
    

    出于效率和其他原因,您可能需要考虑使用 keys_unsorted 而不是 keys