-3

I am getting below sample json data as input,

var JSONDATA =
    {
        "MainNode": {
            "_attributes": { 
                "class": "ABC",
                "projectclass": "MyProject",
                "prjname": "PrjName",
                "enabled":"true"
            },
            "PrjProp": {
                "_attributes": {
                    "name": "MyProject.save" 
                },
                "_text": true 
            }
        }
    }

Using Jquery or Javascript, I want to get the "projectclass" value in first "_attributes". There could be multiple "_attributes" in JSON object but the requirement to the "projectclass" (fixed) from the first (fixed) "_attributes" only.

This can be achieved like, console.log(JSONDATA.MainNode._attributes.testclass);

but "MainNode" is not fixed, this can be "OtherNode". So how to handle this is variable ? I tried , console.log(Object.keys($scope.testplan)[0]); which shows main node name but how to use this in console.log(JSONDATA.MainNode._attributes.testclass); as variable ?

Please suggest. Thanks

usersam
  • 1,125
  • 4
  • 27
  • 54

2 Answers2

2

You have to access as JSONDATA.MyProject._attributes.projectclass

Using [0] is accessing the first element of an array. This is an object, so that is the reason why you are not able to access JSONDATA[0][0].projectclass

var JSONDATA = {"MyProject":{"_attributes":{"class":"ABC","projectclass":"MyProject","prjname":"PrjName","enabled":"true"},"PrjProp":{"_attributes":{"name":"MyProject.save"},"_text":true}}};

console.log( JSONDATA.MyProject._attributes.projectclass );

And there is going to be only 1 _attributes under MyProject.


If you have multiple projects, You can loop by:

var JSONDATA = {
    "MyProject":{"_attributes":{"class":"ABC","projectclass":"MyProject","prjname":"PrjName","enabled":"true"},"PrjProp":{"_attributes":{"name":"MyProject.save"},"_text":true}},
    "MyOtherProject":{"_attributes":{"class":"ABC","projectclass":"MyOtherProject","prjname":"PrjName","enabled":"true"},"PrjProp":{"_attributes":{"name":"MyProject.save"},"_text":true}}
};
 
for ( var key in JSONDATA ) {
    console.log( JSONDATA[key]._attributes.projectclass );
}
Eddie
  • 26,593
  • 6
  • 36
  • 58
0

you cant use JSONDATA[0][0] to acces a JSON object for example data is your JSON object to access projectclass simply

  data={
    "MyProject": {
        "_attributes": {
            "class": "ABC",
            "projectclass": "MyProject",
            "prjname": "PrjName",
            "enabled": "true"
        },
        "PrjProp": {
            "_attributes": {
                "name": "MyProject.save"
            },
            "_text": true
        }
    }
}
console.log(data.MyProject._attributes.projectclass);
Abslen Char
  • 3,071
  • 3
  • 15
  • 29
  • It Works. But i missed to mention one thing that main node ""MyProject"" may change in inputs. So we can not hard code to "data.MyProject._attributes.projectclass" because it may be "data.OtherProject._attributes.projectclass" and there will be one project only. I apologize for incomplete information. – usersam Mar 18 '18 at 15:43