gopi
gopi

Reputation: 57

How to build a json file and extract data values from it in jquery?

I have excel sheet which i need to build the json file. i have build a sample file. but i am unable to proceed with that json file and extract the data from it.

[{
   "Material thickness": "10 Mil",        
   "Width": 8,
   "height": 8,
   "Price": "8.76",
   "Quantity":1
},
{
   "Material thickness": "7 Mil",        
   "Width": 8,
   "height": 12,
   "Price": "10.52",
   "Quantity":1
}
]

jQuery.getJSON( siteURL+"/prices.json", function( data ) {
  jQuery.each(data, function (index, value) {
    console.log(index);
    console.log(value);
    console.log(data[index].Material thickness);                             
  });                           
}); 

Upvotes: 0

Views: 293

Answers (2)

Zenoo
Zenoo

Reputation: 12880

You can't use the dot notation when your key contains a space.

The property accessed with a dot notation must be a sequence of alphanumerical characters, also including the underscore ("_") and dollar sign ("$"), that cannot start with a number

Use the bracket notation instead :

data[index]['Material thickness']

var data = [{
    "Material thickness": "10 Mil",
    "Width": 8,
    "height": 8,
    "Price": "8.76",
    "Quantity": 1
  },
  {
    "Material thickness": "7 Mil",
    "Width": 8,
    "height": 12,
    "Price": "10.52",
    "Quantity": 1
  }
];

$.each(data, function(index, value) {

  //console.log(index);
  //console.log(value);
  console.log(data[index]['Material thickness']);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

Hina Patel
Hina Patel

Reputation: 111

You can use the following:

console.log(data[index]["Material thickness"]);

Upvotes: 1

Related Questions