I am reading information from a JSON string, and when I execute
console.log(record.seenTime.time.seenEpoch)
it display the right information.
But when I use console.log(record.seenTime.time[0].seenEpoch), I get an error saying :
TypeError: Cannot read property 'seenEpoch' of undefined.
Here is an example set of data:
{
seenTimes: {
time: {
seenTime: '2014-09-10T20:18:32Z',
seenEpoch: 1410380312
}
}
}
Anyone know what am I doing wrong?
Thanks
record.seenTimes in this case is an Object, not an Array, you can verify that using
typeof record.seenTimes
Because of that, time[0] returns undefined.
seenEpoch is a property of the ONE AND ONLY time object, therefore, you access it using record.seenTimes.time.seenEpoch
For once, I'll recommend reading something from w3schools : JSON Syntax
It'll show you examples of what can be stored in JSON.
EDIT :
Your sample record.seenTimes will not be able to store multiple time objects, as it uses the curly brackets {} which indicate that it's meant to store an object , if you want to be able to store multiple time objects, ie: an Array, your JSON will have to look like :
record {
seenTimes: [
{
time: {
seenTime: '2014-09-10T20:18:32Z',
seenEpoch: 1410380312
}
},
{
time: {
seenTime: '2014-09-10T20:18:32Z',
seenEpoch: 1410380312
}
}
]
}
Note the square brackets that say that seenTime holds an array.
And as slebetman noted :
Also note that in javascript, an object defined as having multiple
identical keys are technically invalid. But most implementations take
the last definition. For example, the object: {a:1,a:2,a:3} is exactly
the same as {a:3}. So if you don't use an array then there is only one
time object even if it appears twice in the JSON string.
Related
I understand that when index names are used to push values in Javascript, they essentially work like objects. But what I don't understand is the following behaviour -
person = [];
person[0] = "Someone";
person["test"] = "SomeoneElse"
Inputting person on the console prints ["Someone"] and I could see no information about person.test.
person.test does print SomeoneElse. However, if I go console.log(person), I get ["Someone", test: "SomeoneElse"].
Curious to check if this makes sense, I tried to create a structure like this one -
var experiment = ["Someone1", test1: "SomeoneElse1"]
and what I get is
Uncaught SyntaxError: Unexpected token
What am I missing?
Thanks in advance!
Typing person on the console prints ["Someone"].
Array.prototype.toString formats this output, and it only considers the "array values" of itself without other properties.
However, if I go console.log(person), I get ["Someone", test: "SomeoneElse"].
console.log outputs other information about the object, including own properties.
Uncaught SyntaxError: Unexpected token
Because that is bogus syntax; the array literal syntax doesn't allow keys, because array values aren't supposed to have keys. Arrays are a numerically indexed list of values. Merely by the fact that under the hood those lists are implemented using objects (because everything in Javascript is an object of some kind or another) are you able to set "non numeric keys" on the array. That doesn't mean you're using the array correctly though.
Also see Are JavaScript Array elements nothing more than Array object properties?
This is because an array in JavaScript is also an object itself, and objects can have properties. So it is perfectly valid to have an array with elements, but also have properties set on the array object.
The second example doesn't work because the [...,...,...] syntax is specifically for instantiating an array and its elements.
typing person in console is like having an alert(person); or passing its value to a variable or element, so it is more like you want to get the first set of readable values. That is the reason why it is showing you the values inside, you can try adding person[1] = *something;*, then it will display someone, something
console.log(person) - it displays all the items inside an object. It is more like informing you of what is inside, like a text visualizer in your IDE
var experiment = ["Someone1", test1: "SomeoneElse1"] - it will absolutely throw an exception there is no such format like this on initializing values, at least it is expecting an array format like var experiment = ["Someone1", "SomeoneElse1"]
I have a variable :
var testData;
And I have a function that populates an array. Goes through an array and makes another array like so :
var person = {
"Name": obj.Name,
"Age": obj.Age,
}
partsObject.push(person);
I then want to make this array into JSON so I can use it with my D3 objects, so I do this :
testData = JSON.stringify(partsObject);
I can console log this variable, but when trying to go through it via D3's forEach method like so :
testData.forEach(function(d) // data is the JSON
{
I get the error Uncaught TypeError: testData.forEach is not a function
I don't understand how I can log the variable to the console yet it's as if I can't use it as JSON. Any ideas ?
As the name suggests stringify() converts a JavaScript object (the JSO in JSON) into a string of JSON. You can console.log() it because console.log expects to take a string, and anything that's not a string is converted to one to be displayed.
If you want to use it as an array again, you need to parse your string of JSON back to the JavaScript object: JSON.parse(testData).
You really dont need to stringify your Array to pass to d3. Do not to get confused with javascript objects, since forEach requires an array to loop through and you are passing a string to manipulate with forEach function
use:
partsObject.forEach(function(d)
{
...
JSON.stringify(partsObject); creates a string as"{'Name':'ABC','Age':23}"
Uncaught TypeError: testData.forEach is not a function caused because javascript was not able to find an Array
.stringify() turns a Javascript Object into a string. You would want to either run
partsObjects.forEach()
or alternativily you could turn the stringify'ed string back into an object with
(JSON.parse(testData)).forEach()
You are currently trying to loop through a String since you stringify your array.
Just do partsObject.forEach and don't stringify your Array.
I have a JS object
{
aString:[aNumber, aURL]
}
JSON.stringify() returns
{
"aString":"[number, \"aURL\"]"
}
I thought that valid JSON can have arrays as values. Can I have stringify return the JSON string without converting the array into a string? Basically I need turn the whole JS object straight into a string, without any modification.
Is there a better way to do this? I've been looking around but everyone suggests using JSON.stringify if I want an object to string, and no one has raised this problem.
EDIT: Thanks for the quick responses. Here is how I created my JS object, please let me know if I messed up and how!
cookie = {};
// productURL is a string, timer is a number, imageSrc is a URL string
cookie[productURL] = [timer, imageSrc];
// then, I just stringified cookie
newCookie = JSON.stringify(cookie);
If it is also relevant, I am setting an actual cookie's value as the resulting JSON string, in order to access it in another set of functions. Setting the cookie's value does do some URI encoding of its own, but I've actually been grabbing the value of newCookie in the Chrome console as well and it also returns the Array as a string.
If an object you're trying to stringify has a toJSON function, that will be called by JSON.stringify. Most likely you have an external library that's adding Array.prototype.toJSON.
For example, an old version (1.6) of Prototype JS will "conveniently" add that for you.
Prototype 1.6.1:
alert(JSON.stringify([1, 2, 3]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/prototype/1.6.1/prototype.min.js"></script>
Whereas a newer version will not.
Prototype 1.7.2:
alert(JSON.stringify([1, 2, 3]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/prototype/1.7.2/prototype.min.js"></script>
You could try deleting Array.prototype.toJSON just to see if that's what's causing the problem. If it is, you might want to look into upgrading/deprecating any libraries in your code that do weird things like that.
Prototype 1.6.1 (after deleting toJSON)
delete Array.prototype.toJSON;
alert(JSON.stringify([1, 2, 3]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/prototype/1.6.1/prototype.min.js"></script>
Based on your description this is not what should happen.
If you have code like this:
var obj = {
aString:[123, "test"]
}
document.getElementById("out").value = JSON.stringify(obj);
it will generate the expected json:
{"aString":[123,"test"]}
also see https://jsfiddle.net/zudrrc13/
in order to produce your output the original object would have to look something like:
var obj = {
aString:"[123, \"test\"]"
}
a Services provides the following Information via JSON
{"errors":{"subject":"foobar"}}
and further alements in this errors array
OR
{"ok":{"subject":"foobarfoobar"}}
and further alements in this ok array array
So always either of the two is present, not both at once. Any as one is not present I always get an access error as one the proeprties is of course not existing
And I completely fail how to process this result. I always get "can not ... of undefined":
Currenlty I have:
if (data[0].hasOwnProperty("status")) {
alert('ddd');
}
What is the correct way to test, if either errors or ok is provided?
UPDATE
blex is right: But on the serverside is use:
echo json_encode( array('errors' => $result['errors']) );
Doesnt this mean I that errors is already the first element of an array? Why is this seen as an object propety?
SOLUTION/LEARNING:
Beware of PHP! As stated above echo json_encode( array('errors' => $result['errors']) ); does NOT lead to encoding this as an JSON Array. What I missunderstood and oversaw was, that { is the Format/Notation for Objects NOT for Arrays. So to be an array it would have to be [ . So in the End PHP does encode Associative Arrays as normal Object Properties not as arrays. That was my wrong assumption
var x = { "errors": { "subject": "foobar" } };
//var x = { "ok": { "subject": "foobarfoobar" } };
if (x.errors) {
} else if (x.ok) {
}
Essentially, you're already on the right track. hasOwnProperty is the right way to check for the existence of these keys.
I'm not sure what language you're using, but it seems like json_encode is converting your array to a single object, then putting that object under the errors key.
One last thing, you should probably look into using HTTP response codes. If you can return a 200 OK for the okay scenario, and a 400 or 500 level code for your error, then you'll be able to use better methods of determining how to process these situation. jQuery.ajax has the ability to put two handlers in place, one for success and one for failure, for instance.
I am receiving a JSON object from a http call and I am trying to extract values from it.
JSON object contains:
data:{"userid":"007", "role":"spy"}
I use the following code to assign role property to another variable followed by some console log checks:
currentUserRole = data.role;
console.log("type of data: "+typeof(data));
console.log("data: "+JSON.stringify(data));
console.log("user role: "+currentUserRole);
The logs produce:
type of data: object
data: [{"userid":"007", "role":"spy"}]
user role: undefined
Also I tried another method of assignment:
currentUserRole = data['role'];
But currentUserRole remains undefined. How can I set a property of a JSON object to a variable?
According to the second line of your log (the call to JSON.stringify()), your data is actually an array of objects:
[{"userid":"007", "role":"spy"}]
If it was an object as you are expecting, it would look like this:
{"userid":"007", "role":"spy"}
(the difference is subtle, but notice the missing square brackets)
Try this:
currentUserRole = data[0].role;
Obviously in production-ready code, you probably need to do some extra sanity checking to ensure that data is in fact an array containing at least one element.
It is a list. Try data[0].role