0

I have to access property on object:

var jsonobj= {
    "first": {
        "second": 120
    }
}

How to check if second is available or not? jsonobj.hasOwnProperty() returns false if we check here.

If I want to change the value of second from 120 to 100, how can I achieve it? Can I get a generalized solution, so that it works for any number of hierarchies?

Edit: What if I don't know the name of the property?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Navaneeth
  • 2,555
  • 1
  • 18
  • 38
  • 2
    `jsonobj.first.second = 100;` will do it. As to a generalised solution, how do you picture that working for duplicate property names? The object could have several properties that all reference other objects that each have their own `second` property, or the same property name could be used at several levels such that `obj.first.second.second.second` is not the same as `obj.first.second` - so which would you return? Also, this is _not_ JSON, it's an object (or nested objects) - [there's no such thing as a JSON object](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). – nnnnnn Sep 10 '12 at 06:18
  • @nnnnnn : thanx for ur link. I just understood the difference between json and object literal! :) – Navaneeth Sep 10 '12 at 07:01
  • Please note that the problem has **nothing** to do with JSON at all. It seems you are confusing JavaScript object literals (constructs of the JavaScript language syntax) with JSON (a language-independent data-exchange format, like XML or CSV). I will edit your question accordingly. See also: [There is no such thing as a "JSON object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). – Felix Kling Oct 02 '13 at 13:47

4 Answers4

2

If you want to check the existence of [the unique path to] a nested property [key], this function may help:

function keyPathExists(obj,keypath){
  var keys = keypath.split('.'), key, trace = obj;
  while (key = keys.shift()){
    if (!trace[key]){
      return null
    };
    trace = trace[key];
  }
  return true;
}

//usages
var abcd = {a:{b:{c:{d:1}}}};
keyPathExists(abcd,'a.b.c.d'); //=> 1
keyPathExists(abcd,'a.b.c.d.e'); //=> null
if (keyPathExists(abcd,'a.b.c.d')){
 abcd.a.b.c.d = 2;
}

Please read @nnnnnns comment, especially the provided link within it carefully.

Community
  • 1
  • 1
KooiInc
  • 119,216
  • 31
  • 141
  • 177
0

Use typeof:

if(typeof jsonobj.first == 'undefined'){
    jsonobj.first = {};
}
if(typeof jsonobj.first.second == 'undefined'){
    jsonobj.first.second = {};
}    
jsonobj.first.second = 100;
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
0

To change the value, you can use the "dot" notation - so try: jsonobj.first.second = 100

Lasse Christiansen
  • 10,205
  • 7
  • 50
  • 79
0
var jsonobj= {
              "first": {
                           "second": 120
                        }
           }

alert(jsonobj.first.second);

jsonobj.first.second = 100

alert(jsonobj.first.second);​
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95