4

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?

3
  • What value would you expect for obj[0]? Commented Aug 23, 2016 at 6:51
  • 1
    It is an hash, not an array. What do you expect when you write obj[0]? 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"] or obj.key1, you can only used numeric indexes when using Arrays: [ .. ] Commented Aug 23, 2016 at 6:54

6 Answers 6

3

You just can't access named object property by index. You can use obj[Object.keys(obj)[0]]

Edit:

As @smarx explained in the comments, 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' }
Sign up to request clarification or add additional context in comments.

12 Comments

I don't think Object.keys guarantees any particular ordering, so there's no way to predict which key you would be accessing here, right? (So I can't see how this code would be useful.)
There was nothing about ordering in the question if i have no any misunderstanding. It is suitable for looping trough object properties, for "getting value", not for direct access to the specific property.
The snippet you shared will give a random value from the object. (Random in the sense that it can't be predicted.) Do you actually think that will help the person who asked this question?
Snippet i shared just shows how to access values, as it was requested. You can also use Object.keys(obj)[0] a key name, depends what do you want to do, right?
How do I loop keys values of the JSON object as I would be getting JSON object and would have no clue of the Key names
|
2

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.

2 Comments

You mean "obj is an object not an array." string is a string. :-)
Sorry it is my mistake to consider json as array
2

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

Comments

2

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.net/kbwbspnk/

For nested property value you have to use . operator.

console.log(obj.Key3.key31); // value 31

2 Comments

Thanks Rohit. Actually I would be getting a JSON object and I would have no clue of key names.
@JoelDivekar Ok as per your requirement you can try this solution.
1

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.

1 Comment

That was my mistake smarx as I was considering JSON object as an array
0

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

1 Comment

I think you mean "obj is not an array. It's an object." (string is a string.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.