1

I need to create a table from selected data generated by some rental software in json format via an api. The json data supplied is a list of products, and is quite extensive with objects containing other objects and arrays.

The problem comes when I need to retrieve the data from an object within an object. I want to get Name, weight and Rental Price form the data below. The rental price is in the rental_rate object.

{
"products": [
{
  "id": 1404,
  "name": "Product 1",
  "type": "Product",
  "tag_list": [],
  "description": "",
  "allowed_stock_type": 1,
  "allowed_stock_type_name": "Rental",
  "stock_method": 1,
  "stock_method_name": "Bulk",
  "buffer_percent": "50.0",
  "post_rent_unavailability": 0,
  "replacement_charge": "0.0",
  "weight": "0.5",
  "barcode": "@st2078",
  "active": true,
  "accessory_only": false,
  "system": false,
  "discountable": true,
  "rental_rate": {
    "item_id": 1404,
    "store_id": 1,
    "transaction_type": 1,
    "rate_definition_id": 3,
    "price": "10.0",
    "deposit": "0.0",
    "category_prices": [],
    "properties": {
      "day_cost": "0.0",
      "day_price": "0.0",
      "hour_cost": "0.0",
      "hour_price": "0.0",
      "__value_types": "---\nday_price: BigDecimal\nhour_price: BigDecimal\ndistance_price: BigDecimal\nflat_rate_price: BigDecimal\nday_cost: BigDecimal\nhour_cost: BigDecimal\ndistance_cost: BigDecimal\nflat_rate_cost: BigDecimal\n",
      "distance_cost": "0.0",
      "distance_price": "0.0",
      "flat_rate_cost": "0.0",
      "flat_rate_price": "0.0"
    },
    "priority": 0,
    "date_range": "1900-01-01...3000-01-01"
  },
  "sale_rate": null,
  "product_group_id": 5,
  "tax_class_id": 2,
  "rental_revenue_group_id": 1,
  "sale_revenue_group_id": null,
  "created_at": "2017-02-06T00:49:47.755Z",
  "updated_at": "2017-02-06T00:49:47.755Z",
  "custom_fields": {
    "barcode_notes": "Actual Leg Length is: 3'",
    "product_serial_number": ""
  },
  "product_group": {
    "id": 5,
    "name": "Staging",
    "description": "",
    "created_at": "2017-02-05T22:20:53.465Z",
    "updated_at": "2017-02-05T22:20:53.465Z",
    "custom_fields": {}
  },
  "tax_class": {
    "id": 2,
    "name": "VAT Standard"
  },
  "icon": null,
  "rental_revenue_group": {
    "id": 1,
    "name": "Rental",
    "description": "",
    "active": true
  },
  "sale_revenue_group": null,
  "accessories": [],
  "alternative_products": [],
  "attachments": [],
  "rental_rates": [
    {
      "id": 1782,
      "store_id": null,
      "store_name": "",
      "rate_definition_id": 3,
      "rate_definition_name": "3 Day Week Rate",
      "starts_at": null,
      "ends_at": null,
      "price": "10.0",
      "category_prices": []
    }
  ],
  "sale_rates": []
},
{
  "id": 2395,
  "name": "Product 2",
  "type": "Product",
  "tag_list": [],
  "description": "",
  "allowed_stock_type": 1,
  "allowed_stock_type_name": "Rental",
  "stock_method": 2,x
  "stock_method_name": "Serialised",
  "buffer_percent": "50.0",
  "post_rent_unavailability": 0,
  "replacement_charge": "0.0",
  "weight": "45.0",
  "barcode": "",
  "active": true,
  "accessory_only": false,
  "system": false,
  "discountable": true,
  "rental_rate": {
    "item_id": 2395,
    "store_id": 1,
    "transaction_type": 1,
    "rate_definition_id": 3,
    "price": "0.0",
    "deposit": "0.0",
    "category_prices": [],
    "properties": {
      "day_cost": "0.0",
      "day_price": "0.0",
      "hour_cost": "0.0",
      "hour_price": "0.0",
      "__value_types": "---\nday_price: BigDecimal\nhour_price: BigDecimal\ndistance_price: BigDecimal\nflat_rate_price: BigDecimal\nday_cost: BigDecimal\nhour_cost: BigDecimal\ndistance_cost: BigDecimal\nflat_rate_cost: BigDecimal\n",
      "distance_cost": "0.0",
      "distance_price": "0.0",
      "flat_rate_cost": "0.0",
      "flat_rate_price": "0.0"
    },
    "priority": 0,
    "date_range": "1900-01-01...3000-01-01"
  },
  "sale_rate": null,
  "product_group_id": 6,
  "tax_class_id": 2,
  "rental_revenue_group_id": 1,
  "sale_revenue_group_id": null,
  "created_at": "2017-02-06T00:50:35.834Z",
  "updated_at": "2017-02-06T00:50:35.834Z",
  "custom_fields": {
    "barcode_notes": "",
    "product_serial_number": ""
  },
  "product_group": {
    "id": 6,
    "name": "Cases",
    "description": "",
    "created_at": "2017-02-05T22:20:53.509Z",
    "updated_at": "2017-02-05T22:20:53.509Z",
    "custom_fields": {}
  },
  "tax_class": {
    "id": 2,
    "name": "VAT Standard"
  },
  "icon": null,
  "rental_revenue_group": {
    "id": 1,
    "name": "Rental",
    "description": "",
    "active": true
  },
  "sale_revenue_group": null,
  "accessories": [],
  "alternative_products": [],
  "attachments": [],
  "rental_rates": [
    {
      "id": 2773,
      "store_id": null,
      "store_name": "",
      "rate_definition_id": 3,
      "rate_definition_name": "3 Day Week Rate",
      "starts_at": null,
      "ends_at": null,
      "price": "0.0",
      "category_prices": []
    }
  ],
  "sale_rates": []
}
  ],
 "meta": {
"total_row_count": 1376,
"row_count": 2,
"page": 1,
"per_page": 2
   }
  }

and here is the JS I am using to get the data:

$(document).ready( function() {
$.getJSON( 'https://myapi.com/products', function(data) { 
  $.each(data.products, function() { 
     $("table#prod").append("<tr><td>" + this['name'] + "</td><td>" + this['rental_rate.price'] + "</td><td>" + this['weight'] + "kg</td></tr>");
  }); }); });

This works fine for name and weight, but not rental price. I have done a thorough search of most places on the internet and not found an answer - my javascript knowledge is limited, which probably means I am going about this all wrong, or not describing it well enough... Any help or improved methods would be much appreciated!

audiojames
  • 82
  • 1
  • 11
  • You need `this['rental_rate']['price']` or `this.rental_rate['price']` or `this.rental_rate.price` Two minutes of basic research would've yielded this information. –  Feb 06 '17 at 19:28
  • @ChrisG - is it possible to downvote a comment? – Adam Jenkins Feb 06 '17 at 19:30
  • @Adam No. Two minutes of basic research would've yielded this information. –  Feb 06 '17 at 19:31
  • Thanks both of you, I think this was down to me not understanding what the square braces meant, so I was searching for the wrong question. I now know more. – audiojames Feb 06 '17 at 19:44

1 Answers1

3

this.name gets you the name

this.rental_rate gets you the nested rental rate object.

this.rental_rate.price gets you the price.

You can similarly use square bracket notation, but people often only do this when dot syntax won't work (because of spaces in the key).

e.g.

this["name"]

this["rental_rate"]["price"]

this["Invalid Key when accessed via dot syntax but fine with square brackets"]

Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
  • Thanks for the fast response Adam, and for solving my problem. I had totally misunderstood what I was doing! – audiojames Feb 06 '17 at 19:35