jq选择路径中的动态数据

时间:2018-02-12 12:03:14

标签: jq

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

所以我写了这个._source.offers_data [] | select(.shop_id == ._source.search_result_data.shop.id).shop_name as ._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, “状态”:1, “注意”: “0.0000”, “is_exclusivity”:NULL, “is_favorite”:假 “is_new”:假 “is_topsales”:假,“价格“:249.99,” promo_price “:249.99,” grey_dot “:真” best_shop_name “:” 东西“}

1 个答案:

答案 0 :(得分:0)

jq 解决方案:

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

输出:

"Stuff"