Cannot Access Json Object Property Returns Undefined

cannot access json object property returns undefined

You've to parse your json like this. Ensure that your whatever input you're giving to JSON.parse, it should be a string.

You can run the below snippet to ensure that it's working and giving output Hi.

var json = '{"body":"Hi","date":"2016-07-29 07:43:00"}';
var a = JSON.parse(json);document.write(a.body);

javascript says JSON object property is undefined although it's not

Looks like your json object is not really an object, it's a json string. in order to use it as an object you will need to use a deserialization function like JSON.parse(obj). Many frameworks have their own implementation for deserializing a JSON string.

When you try to do alert(obj) with a real object the result would be [object Object] or something like that

JSON property returning undefined

I've never used stringify() function but I've read the docs and it says it converts a JSON object into a String. You are trying to access a String property so it returns undefined.

Try to comment or delete the following line:

catalog = JSON.stringify(catalog, null, 4);

What you want is a JSON object, not a string ;)

Can't access property from JavaScript object

Probably your response.data object is a string, in this case you have to parse it into a JSON object to be enable to access the properties.

object = JSON.parse(response.data);
console.log('data url', object.url);

Can't access object property, even though it shows up in a console log

The output of console.log(anObject) is misleading; the state of the object displayed is only resolved when you expand the Object tree displayed in the console, by clicking on >. It is not the state of the object when you console.log'd the object.

Instead, try console.log(Object.keys(config)), or even console.log(JSON.stringify(config)) and you will see the keys, or the state of the object at the time you called console.log.

You will (usually) find the keys are being added after your console.log call.

JSON object recieved from api property returns undefined

try this:

console.log("data:", JSON.stringify(result.products[0], null, 2));

console.log to print the result to your console. Use Chrome and developer tools and you will see a console option - excellent for debugging.

JSON.stringify turns the JSON data into something you can see and read. You can convert the data and then split as you need this way too.

OK, a second answer. I can't check this, as I don't have your JSON data, however, I would try something that would likely resemble this...

data.Items[0].field

if the data is not correctly formatted then take the stringify approach and split it out. Otherwise, consider this:

products":[{"title":"Test Product 1"

variable = products[0].title;

I would tend to use a function to pull all the data out in one shot.

function Getv(data){    global.myApp.v = JSON.stringify(data, null, 2); }

then I might run this...

 Getv(data.Items[0]); let splitData = global.myApp.v.split('\"'); let vCount= splitData.length; 

Why I can't access json properties with dot (.) operator while passing a var through $.getjson()

When you want to access an object's (JSON) property dynamically (using a variable or expression), you should use square bracket notation.

[your expression or variable inside square brackets]. i.e, json[useridno]

 var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json) {
console.log(json[usersidno]); // try to use square brackets
});

Accessing data from JSON returns "undefined"

You can't access a property of an object in the array like this this.state.data.date.
Javascript will look for data in Array, not in object inside data array.

To get to an object in Array you can do:

this.state.data[0].date.

But I believe you want to modify code:

<section>
{this.state.dayview && this.state.data.filter(x =>
x.date === this.state.dayview[0].date).map(item => {
return <DayView
key={item.index}
date={item.date}
high={item.high}
low={item.low}
medium={item.medium} />
})
}
</section>

dayview is an array that's why:

this.state.dayview[0].date

JSON Object Property not returnable - "Cannot Read Property of Undefined"

This code works for me:

localStorage.setItem("user", JSON.stringify({
"OrderId":0,
"IsLoggedIn":true,
"ModeOfSaleId":64,
"OriginalModeOfSaleId":64,
"SourceId":8580,
"LoginInfo":{"ConstituentId":190554,"OriginalConstituentId":190554,"UserId":"test@email.org","Status":"P","FailedAttempts":0,"LockedDate":null,"ElectronicAddress":"test@email.org"},
"CartInfo":{"PerformanceCount":0,"PackageCount":0,"ContributionCount":0,"MembershipCount":0,"UserDefinedFeeCount":0,"GiftCertificateCount":0,"PaymentCount":0,"FirstSeatAddedDateTime":null},
"BusinessFacing":false,
"IsGuest":false,
"CheckoutStatus":{"Status":"No Checkout","Date":null},
"HasLockedSeats":false,
"SeatsExpired":false
}));

const user = JSON.parse(localStorage.getItem("user"));

console.log(user.LoginInfo.OriginalConstituentId);



Related Topics



Leave a reply



Submit