【问题标题】:jq select with dynamic data from pathjq 从路径中选择动态数据
【发布时间】:2018-02-12 12:03:13
【问题描述】:

._source.offers_data[]|select(.shop_id == 2004).shop_name 有效,但我想用动态值 ._source.search_result_data.shop.id 替换 2004

所以我写了这个 ._source.offers_data[]|select(.shop_id == ._source.search_result_data.shop.id).shop_name 因为 ._source.search_result_data.shop.id 是一个数字,它似乎不是类型问题

{
"_shards": {
    "failed": 0, 
    "successful": 2, 
    "total": 2
}, 
"hits": {
    "hits": [
                {
            "_id": "P3ssss", 
            "_index": "truc", 
            "_score": 0.0057305535, 
            "_source": {
                "category": {
                    "all_categories": [
                        {
                            "id": 69, 
                            "name": "my stuff", 
                            "url": "/fr/soms/"
                        }
                    ], 
                    "master_category": {
                        "id": 69, 
                        "name": "my stuff", 
                        "url": "/soms/"
                    }, 
                    "master_path": "28-63-69", 
                    "master_universe": {
                        "id": 28, 
                        "name": "ffffff", 
                        "url": "/soms/"
                    }, 
                    "paths": [
                        "28-63-69"
                    ]
                }, 
                "number_filters": {
                    "available_quantity": 3, 
                    "status": 1
                }, 
                "offers_data": [

                    {
                        "availability_date": "2018-02-14", 
                        "base_price": 280.06, 
                        "nature": 11, 
                        "offer_id": 2628, 
                        "promo_price": 247.35, 
                        "quantity": 3, 
                        "shipping_price": 0, 
                        "shop_grade": 3, 
                        "shop_id": 2004, 
                        "shop_name": "Stuff", 
                        "shop_url": "/some/Stuff"
                    }, 
                    {
                        "availability_date": "2018-02-14", 
                        "base_price": 270.61, 
                        "nature": 11, 
                        "offer_id": 4998, 
                        "promo_price": 270.61, 
                        "quantity": 12, 
                        "shipping_price": 0, 
                        "shop_grade": 3, 
                        "shop_id": 2003, 
                        "shop_name": "Truc", 
                        "shop_url": "/some/truc"
                    }
                ], 
                "search_result_data": {
                    "base_price": 280.06, 
                    "brand": {
                        "id": 58, 
                        "logo": "/so.jpg", 
                        "name": "truc", 
                        "url": "/TTTT"
                    }, 
                    "flags": {
                        "is_exclusivity": null, 
                        "is_favorite": false, 
                        "is_new": false, 
                        "is_topsales": false
                    }, 
                    "grey_dot": true, 
                    "identifiers": {
                        "erp": "ZORGLUB", 
                        "id": 455565
                    }, 
                    "image": "/images/", 

                    "name": "Isomething", 
                    "nature": 11, 
                    "offers_count": 11, 
                    "offers_min_price": 233.21, 
                    "own_offer": {
                        "base_price": 249.99, 
                        "base_price_ht": 208.325, 
                        "grey_dot": true, 
                        "nature": 11, 
                        "promo_price": 249.99, 
                        "promo_price_ht": 208.325
                    }, 
                    "promo_price": 247.35, 
                    "reviews_count": "0", 
                    "reviews_count_website_5": "0", 
                    "reviews_note": "0.0000", 
                    "reviews_note_website_5": "0.0000", 
                    "shop": {
                        "id": 2004, 
                        "name": "Stuff", 
                        "url": "/soms/Stuff"
                    }, 
                    "url": "/soms/product.html"
                }
            }, 
            "_type": "page", 
            "sort": [
                0.0057305535, 
                0.04427508529513
            ]
        }
    ], 
    "max_score": null, 
    "total": 3657
}, 
"timed_out": false, 
"took": 68
}

那么我必须在该语句中使用:

cat my.json | jq -c '.hits.hits[]|{products_ids: ._source.search_result_data.identifiers.id,best_shop_name: ._source.offers_data[]|select(.shop_id ==  ._source.search_result_data.shop.id)}

所以最终结果应该是:

{"products_ids":30671,"status":1,"note":"0.0000","is_exclusivity":null,"is_favorite":false,"is_new":false,"is_topsales":false,"price ":249.99,"promo_price":249.99,"grey_dot":true,"best_shop_name":"Stuff"}

【问题讨论】:

  • 发布你的输入 json 结构

标签: jq


【解决方案1】:

jq解决方案:

jq '.hits.hits[]._source | .search_result_data.shop.id as $shop_id 
    | .offers_data[] | select(.shop_id == $shop_id).shop_name' jsonfile

输出:

"Stuff"

【讨论】:

  • @glitchtracker,将最终预期结果添加到您的问题中
  • 但是如何把它放进去:cat my.json | jq -c '.hits.hits[]|{products_ids: ._source.search_result_data.identifiers.id,best_shop_name: ._source.offers_data[]|select(._source.search_result_data.shop.id==.shop_id).shop_name}
  • 要使其在 {key_name: } 结构中工作,您需要加上括号。所以最终结果是: cat my.json | jq -c '.hits.hits[]|{products_ids: ._source.search_result_data.identifiers.id,best_shop_name: (._source.search_result_data.shop.id as $shop_id|._source.offers_data[]|select(.shop_id= =$shop_id).shop_name)}'
猜你喜欢
  • 2016-09-30
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-01
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多