Home>

I would like to know how to search by tracing the hash hierarchy which is the return value of API in Ruby.

Of the hash like below
"id" of "thumbnail": "1013",
I want to extract the corresponding data. It seems that you can not do it with hash find or select
If anyone knows, please let me know.

* Hash for extraction (part)

{
  "data": [
    {
      "id": "1192",
      "type": "product",
      "attributes": {
        "id": 1192,
        "number": "test_001",
        "state": "active",
        "human_state": "display",
        "name": "test",
        "upsell_product_id": 1198,
        "upsell_product_number": "upsell",
        "upsell_product_name": "upsell",
        "cv_upsell_product_id": 1309,
        "cv_upsell_product_number": "thanks_upsell",
        "cv_upsell_product_name": "Thanks Upsell",
        "maker_id": null,
        "maker_name": null,
        "description": "",
        "description_mobile": "",
        "sub_description": "",
        "sub_description_mobile": "",
        "meta_description": "",
        "meta_keywords": "",
        "is_recurring": true,
        "product_category_names": "",
        "master_list_price": 7500,
        "master_sales_price": 2500,
        "master_sku": "smart_teiki_002-001",
        "created_at": "2019/09/30 14:55:00",
        "updated_at": "2020/04/17 01:47:48",
        "deleted_at": null
      },
      "relationships": {
        "variants": {
          "data": [
            {
              "id": "1723",
              "type": "variant"
            },
            {
              "id": "1722",
              "type": "variant"
            },
            {
              "id": "1721",
              "type": "variant"
            },
            {
              "id": "1720",
              "type": "variant"
            },
            {
              "id": "1589",
              "type": "variant"
            }
          ]
        },
        "thumbnail": {
          "data": {
            "id": "1013",
            "type": "thumbnail"
          }
        }
      }
    }
  ],,
  "included": [
    {
      "id": "1013",
      "type": "thumbnail",
      "attributes": {
        "id": 1013,
        "file_name": "test.png",
        "content_type": "image/png",
        "file_size": 599789,
        "position": 0,
        "url": "https://example.com/28cd4d9bc4/ec_assets/original.png?1587703559",
        "url_small": "https://example.com/28cd4d9bc4/ec_assets/small.png?1587703559",
        "url_medium": "https://example.com/28cd4d9bc4/ec_assets/medium.png?1587703559",
        "url_large": "https://example.com/28cd4d9bc4/ec_assets/large.png?1587703559",
        "path": "/28cd4d9bc4/ec_assets/original.png",
        "path_small": "/28cd4d9bc4/ec_assets/small.png",
        "path_medium": "/28cd4d9bc4/ec_assets/medium.png",
        "path_large": "/28cd4d9bc4/ec_assets/large.png",
        "created_at": "2020/04/24 13:46:00",
        "updated_at": "2020/04/24 13:46:00"
      }
    }
  ],,
  "meta": {
    "total_count": 1,
    "page": 1,
    "per": 20, 20,
    "count": 1,
    "total_pages": 1
  },
  "links": {
    "self": "http: // localhost: 3000/api/v1/products? page = 1&per = 20",
    "prev": null,
    "first": "http: // localhost: 3000/api/v1/products? Page = 1&per = 1",
    "next": "http: // localhost: 3000/api/v1/products? Page =&per = 20",
    "last": "http: // localhost: 3000/api/v1/products? page = 1&per = 20"
  }
}
  • Answer # 1

    ・ I want to get the data of the "thumbnail" key from the data of this structure.
    Will be.

    If so, specify the keys or subscripts in order.

    a = ~~~
    p a ["data"] [0] ["relationships"] ["thumbnail"]
    # =>{"data" =>{"id" =>"1013", "type" =>"thumbnail"}}

Related articles