Reputation: 17
$.getJSON(staticMS, function(data) {
stockData = data.products_and_categories;
console.log(stockData);
var typeData = JSON.parse(stockData)
console.log(typeData.itemType);
});
Not sure whats wrong with this, I keep getting a "Uncaught SyntaxError: Unexpected token o in JSON at position 1" error. Trying to access part (itemType is specific earlier in the script) under the products_and_categories part of JSON file.
Tables under products and categories (after i click on 'object' in console):
Accessories: (8) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Hats: (6) [{…}, {…}, {…}, {…}, {…}, {…}]
Shirts: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
Pants: (5) [{…}, {…}, {…}, {…}, {…}]
Upvotes: 0
Views: 700
Reputation: 11574
As Sajeeb commented, "stockData
is not a valid JSON string." In fact, stockData
is likely a JS object because
1) it's a field on parsed JSON, and JSON doesn't usually contain nested JSON
2) JSON.parse
casts it's param to a string. A JS object stringified is '[object Object]'
. Parsing this would produce the error you saw, 'Unexpected token o in JSON at position 1'.
If my assumption is right, then all you need to do is remove the nested JSON.parse
:
$.getJSON(staticMS, function(data) {
stockData = data.products_and_categories;
console.log(stockData);
var typeData = stockData;
console.log(typeData.itemType);
});
Upvotes: 1