RubyでAPIの戻り値であるハッシュの階層をたどって検索する方法を知りたいです。
下記のようなハッシュの
"thumbnail"の"id": "1013",
にあたるデータを抽出したいです。ハッシュのfindやselectでは出来なそうなので
どなたかご存じの方ご教示ください。
※抽出用ハッシュ(一部)
Ruby
1{ 2 "data": [ 3 { 4 "id": "1192", 5 "type": "product", 6 "attributes": { 7 "id": 1192, 8 "number": "test_001", 9 "state": "active", 10 "human_state": "表示", 11 "name": "テスト", 12 "upsell_product_id": 1198, 13 "upsell_product_number": "upsell", 14 "upsell_product_name": "アップセル", 15 "cv_upsell_product_id": 1309, 16 "cv_upsell_product_number": "thanks_upsell", 17 "cv_upsell_product_name": "サンクスアップセル", 18 "maker_id": null, 19 "maker_name": null, 20 "description": "", 21 "description_mobile": "", 22 "sub_description": "", 23 "sub_description_mobile": "", 24 "meta_description": "", 25 "meta_keywords": "", 26 "is_recurring": true, 27 "product_category_names": "", 28 "master_list_price": 7500, 29 "master_sales_price": 2500, 30 "master_sku": "smart_teiki_002-001", 31 "created_at": "2019/09/30 14:55:00", 32 "updated_at": "2020/04/17 01:47:48", 33 "deleted_at": null 34 }, 35 "relationships": { 36 "variants": { 37 "data": [ 38 { 39 "id": "1723", 40 "type": "variant" 41 }, 42 { 43 "id": "1722", 44 "type": "variant" 45 }, 46 { 47 "id": "1721", 48 "type": "variant" 49 }, 50 { 51 "id": "1720", 52 "type": "variant" 53 }, 54 { 55 "id": "1589", 56 "type": "variant" 57 } 58 ] 59 }, 60 "thumbnail": { 61 "data": { 62 "id": "1013", 63 "type": "thumbnail" 64 } 65 } 66 } 67 } 68 ], 69 "included": [ 70 { 71 "id": "1013", 72 "type": "thumbnail", 73 "attributes": { 74 "id": 1013, 75 "file_name": "test.png", 76 "content_type": "image/png", 77 "file_size": 599789, 78 "position": 0, 79 "url": "https://example.com/28cd4d9bc4/ec_assets/original.png?1587703559", 80 "url_small": "https://example.com/28cd4d9bc4/ec_assets/small.png?1587703559", 81 "url_medium": "https://example.com/28cd4d9bc4/ec_assets/medium.png?1587703559", 82 "url_large": "https://example.com/28cd4d9bc4/ec_assets/large.png?1587703559", 83 "path": "/28cd4d9bc4/ec_assets/original.png", 84 "path_small": "/28cd4d9bc4/ec_assets/small.png", 85 "path_medium": "/28cd4d9bc4/ec_assets/medium.png", 86 "path_large": "/28cd4d9bc4/ec_assets/large.png", 87 "created_at": "2020/04/24 13:46:00", 88 "updated_at": "2020/04/24 13:46:00" 89 } 90 } 91 ], 92 "meta": { 93 "total_count": 1, 94 "page": 1, 95 "per": 20, 96 "count": 1, 97 "total_pages": 1 98 }, 99 "links": { 100 "self": "http://localhost:3000/api/v1/products?page=1&per=20", 101 "prev": null, 102 "first": "http://localhost:3000/api/v1/products?page=1&per=1", 103 "next": "http://localhost:3000/api/v1/products?page=&per=20", 104 "last": "http://localhost:3000/api/v1/products?page=1&per=20" 105 } 106}
回答1件
あなたの回答
tips
プレビュー