0

I have this result JSON data for my api call, but when i try to access the data in the attribute "69106658_5" I cant, I am getting "Error: Uncaught SyntaxError: Invalid or unexpected token". I have a copy of what I am running on an online editoe below. I am guessing its because of the attribute contains an underscore.

let results=
    {
    "links": {
        "data": {
            "self": {
                "body": "",
                "content_type": "",
                "href": "/api/v2/nodes/69107289/categories",
                "method": "GET",
                "name": ""
            }
        }
    },
    "results": [
        {
            "data": {
                "categories": {
                    "58652374_10": [
                        "16",
                        "16.0.1",
                        "16.2",
                        "16.2.4"
                    ],
                    "58652374_11": [
                        "English"
                    ],
                    "58652374_12": [
                        "Windows"
                    ],
                    "58652374_13": "2018-11-20T00:00:00",
                    "58652374_2": "Published",
                    "58652374_3": "19",
                    "58652374_4": "Video",
                    "58652374_5": "65",
                    "58652374_6": "How To",
                    "58652374_7": [
                        "basic"
                    ],
                    "58652374_8": "237",
                    "58652374_9": "Content Server"
                }
            }
        },
        {
            "data": {
                "categories": {
                    "69106658_2": "You Tube",
                    "69106658_3": [
                        "End User"
                    ],
                    "69106658_4": [
                        "69106508:7"
                    ],
                    "69106658_5": "https://img.youtube.com/vi/j-aOeCpRvEs/hqdefault.jpg",
                    "69106658_6": false,
                    "69106658_7": "Engineering",
                    "69106658_8": null
                }
            }
        }
    ]
    }

    var lookInto = results.results;

    for( let key in lookInto ) {
     var selectData = lookInto[key].data.categories;
     console.log(selectData);
    }

    console.log( selectData.69106658_5 )
Towkir
  • 3,889
  • 2
  • 22
  • 41
teestunna
  • 197
  • 4
  • 17

2 Answers2

5

Attribute fields that begin with anything other than a letter (and some symbols like _), you have to use bracket notation to access.

Instead of selectData.69106658_5, try selectData['69106658_5']

Cup of Java
  • 1,769
  • 2
  • 21
  • 34
1

The underscore shouldn't cause any problem. If you want to access the property "69106658_5", you should do like this :

results.results[1].data.categories["69106658_5"]

raphaelSeguin
  • 449
  • 3
  • 12