0
    $.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) [{…}, {…}, {…}, {…}, {…}]

7
  • 3
    stockData is not a valid JSON string. Commented Apr 10, 2020 at 18:49
  • 1
    Please share what console.log(stockData); this line shows on console. Commented Apr 10, 2020 at 18:53
  • How should I fix it then? Or how could I access another table under stockData. Commented Apr 10, 2020 at 18:54
  • added what it shows in console, (i had to click on 'object' to get that) Commented Apr 10, 2020 at 18:59
  • 1
    I don't see anything like products_and_categories inside the data object. What I see is there is something valid like data.Accessories Commented Apr 10, 2020 at 19:06

1 Answer 1

1

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);
    });
Sign up to request clarification or add additional context in comments.

3 Comments

When I do that I end up getting an undefined in console.
Perhaps there is no itemType property in typeData. Looking at your question, it looks like the keys in your object are 'hats', 'accessories' etc. If you logged stockData.hats or typeData.hats you'd get an array
when I do .Hats it works but when i use .getItemType or .itemType (not sure what to use), I get undefined, so at the top of my file I have var itemType = "itemType"; then in another onclick function I have var item_type = document.getElementById('item_type').value; and getItemType = localStorage.getItem(itemType); (after I set item_type to itemType), not sure what I'm doing wrong. (The other onclick function works and when I do console.log(getItemType) in this function, it works too)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.