I have been trying to parse nested JSON data and below is my code
var string = '{"key1": "value", "key2": "value1", "Key3": {"key31":"value 31"}}';
var obj = JSON.parse(string);
console.log(obj.key1)
console.log(obj[0]);
And this is the output
$ node try.js
value
undefined
Why I am getting undefined for obj[0]
? How to get value in this case, and also for nested key key31
?
Update Now with the help from @SergeyK and others, I have modified my above code as follows
var string = '{"key1": "value1", "key2": "value2", "key3": {"key31":"value 31"}}';
var obj = JSON.parse(string);
var array = Object.keys(obj)
for (var i = 0; i < array.length; i++) {
console.log(array[i], obj[array[i]]);
}
And the output is as follows
$ node try.js
key1 value1
key2 value2
key3 { key31: 'value 31' }
But for {"key31":"value 31"}
how would I access key key31
and get its value value 31
?
I have been trying to parse nested JSON data and below is my code
var string = '{"key1": "value", "key2": "value1", "Key3": {"key31":"value 31"}}';
var obj = JSON.parse(string);
console.log(obj.key1)
console.log(obj[0]);
And this is the output
$ node try.js
value
undefined
Why I am getting undefined for obj[0]
? How to get value in this case, and also for nested key key31
?
Update Now with the help from @SergeyK and others, I have modified my above code as follows
var string = '{"key1": "value1", "key2": "value2", "key3": {"key31":"value 31"}}';
var obj = JSON.parse(string);
var array = Object.keys(obj)
for (var i = 0; i < array.length; i++) {
console.log(array[i], obj[array[i]]);
}
And the output is as follows
$ node try.js
key1 value1
key2 value2
key3 { key31: 'value 31' }
But for {"key31":"value 31"}
how would I access key key31
and get its value value 31
?
-
What value would you expect for
obj[0]
? – str Commented Aug 23, 2016 at 6:51 -
1
It is an hash, not an array. What do you expect when you write
obj[0]
? – Amiram Korach Commented Aug 23, 2016 at 6:51 -
1
JSON Objects (
{ ... }
) use key based indexes, which means if you want to get"value"
you need to reference it by it's key:obj["key1"]
orobj.key1
, you can only used numeric indexes when using Arrays:[ .. ]
– Paradoxis Commented Aug 23, 2016 at 6:54
6 Answers
Reset to default 2You just can't access named object property by index. You can use obj[Object.keys(obj)[0]]
Edit:
As @smarx explained in the ments, this answer is not suitable for direct access to the specific property by index due to Object.keys
is unordered, so it is only for cases, when you need to loop keys/values of object.
Example:
var string = '{"key1": "value", "key2": "value1", "Key3": {"key31":"value 31"}}';
var obj = JSON.parse(string);
var keysArray = Object.keys(obj);
for (var i = 0; i < keysArray.length; i++) {
var key = keysArray[i]; // here is "name" of object property
var value = obj[key]; // here get value "by name" as it expected with objects
console.log(key, value);
}
// output:
// key1 value
// key2 value1
// Key3 { key31: 'value 31' }
When you tries to access
console.log(obj[0]);
You are actually trying to refer element at very first memory location in an array, but var string
is a hash not array.
Thats why you are getting undefined
.
console.log(obj[0]) will display its value only if obj is an array. For example:
var obj = ["value","value2"];
console.log(obj[0]) --> value
With an object, you need to use the key as identifier.
For nested key key31:
console.log(obj.Key3.key31) --> value 31
Hope to be helpfull
console.log(obj[0]);
is giving undefined
because obj is not an array
. obj[0]
will work only if obj is an array as we are accessing the first index
element from an array.
Example :
var obj = ["val1","val2","val3"];
console.log(obj[0]); // val1
As per requirement :
var string = '{"key1": "value", "key2": "value1", "Key3": {"key31":"value 31"}}';
var obj = JSON.parse(string);
var keyArray = Object.keys(obj); // key1
console.log(obj[(keyArray[0])]); // value
working fiddle :
https://jsfiddle/kbwbspnk/
For nested property value you have to use .
operator.
console.log(obj.Key3.key31); // value 31
I have no idea what you want obj[0]
to do, so I can't help with that.
To get the value for key31, use obj.Key3.key31
, which, when logged, should yield value 31
.
your variable (String) is not array its object in above case and you can not access using indexing like 0,1,2... ,you can access this with its key value name like key1,key2...
example: obj['key1'] $ value obj['Key3']['key31'] $ value 31