Accessing json elements gives undefined error

FutureCake

I have the following JSON:

{
    "list": {
        "q": "raw",
        "sr": "28",
        "ds": "Standard Reference",
        "start": 0,
        "end": 1,
        "total": 1391,
        "group": "",
        "sort": "n",
        "item": [
            {
                "offset": 0,
                "group": "Poultry Products",
                "name": "Chicken, broilers or fryers, dark meat, thigh, meat only, raw",
                "ndbno": "05096",
                "ds": "SR"
            }
        ]
    }
}

But when I try to access elements in this json i keep getting undefined error... I already checked if the JSON is valid and yes the json is valid! Maybe I am accesing the JSON wrong? The JSON is stored in a variable called result. This is how I tried so far:

console.log(result.list.ds);

I also tried:

console.log(result["0"].list.ds);

but both solutions give me the following error:

Uncaught TypeError: Cannot read property 'ds' of undefined

Why is this happening? Any tips or suggestions are welcome!

gyre

Is your JSON stored in your variable as a string? Try:

console.log(JSON.parse(result).list.ds)

The reason I ask is that the text Uncaught TypeError: Cannot read property 'ds' of undefined tells me that your result variable does not have a list property, which your JSON clearly would if it were an object, but it most assuredly would not if you had accidentally forgotten to parse your JSON string.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Accessing elements in JSON file

From Dev

Accessing json array elements?

From Dev

Accessing JSON elements in PHP

From Dev

Accessing elements in JSON array

From Dev

Why accessing this json sub elements by index raises this error?

From Dev

Factory function gives "Error: is undefined

From Dev

Parsing through JSON .. Gives undefined?

From Dev

Not accessing the date picker gives the conversion error

From Dev

Accessing session data in another controller gives error

From Dev

Accessing nested JSON elements in Perl

From Dev

Accessing JSON elements in a Scala Map

From Dev

accessing elements from nlohmann json

From Dev

Traverse and Accessing inner elements in JSON

From Dev

accessing elements of json array in php

From Dev

Nested JSON data accessing elements

From Dev

Error in accessing json data

From Dev

Javascript error in accessing JSON

From Dev

Able to access object returned by service, but accessing its properties gives undefined

From Dev

Undefined error when accessing object in angularJs

From Dev

how to avoid undefined error while accessing service?

From Dev

Extract json array from postgres table gives error: cannot extract elements from a scalar

From Java

SwiftUI MacOS gives Error: Undefined symbol: _main

From Dev

Sensu Handler Gives Undefined Method Error

From Dev

Python program gives internal error: _select is undefined

From Dev

Rails selector gives undefined method error

From Dev

Saving CImage gives Error: identifier "ImageFormatJPEG" is undefined

From Dev

jQueryui timespinner example gives undefined error

From Dev

Building Lua on windows gives undefined reference error

From Dev

Javascript object attribute is accessible but gives undefined error

Related Related

HotTag

Archive