I have the below JSON returned from a DB. How do I refer to the element STOCK without specifying by name? I know I can do NewDataSet.STOCK but this JSON is dynamic.
I have tried NewDataSet[1] but it doesn’t like that. Is there a way to refer to it?
{
"NewDataSet": {
"@": {
"xmlns": "STOCK"
},
"STOCK": {
"STOCK_GROUP": "Default",
"STOCK_CODE": "qwertyuiop",
"FEEDER_OR_TRAY_NUMBER": "1",
"ENABLED": "Enabled",
"WEIGHT": "1",
"PRIORITY": "0",
"STOCK_TYPE": "Paper",
"STOCK_ID": "1",
"VALID_FROM": "1900-01-01T00:00:00+00:00",
"VALID_TO": "1900-01-01T00:00:00+00:00",
"STOCK_IMAGE": {}
}
}
}
Here is an example of 2 or more rows::
{
"NewDataSet": {
"@": {
"xmlns": "SECURITY_GROUPS"
},
"SECURITY_GROUPS": [
{
"GROUP_ID": "100",
"NAME": "Administrators",
"DESCRIPTION": "Access to all aspects of system"
},
{
"GROUP_ID": "101",
"NAME": "Operators",
"DESCRIPTION": "users of device"
}
]
}
}
I then run this javascript:
$.each(data[0], function(key, value) {
columnArr.push({"sTitle" : key});
});
$.each(data, function(key, value) {
var innerArr = [];
$.each(value, function(innerKey, innerValue) {
innerArr.push(innerValue);
});
alueArr.push(innerArr);
});
You can iterate over the elements in a associative array like this:
Looking at the data, they kind of suggest what you should actually do is to explicitly retrieve ‘@’ and then check ‘xmlns’ in there to understand what key you should be searching for…
— edit after your edits it’s clearer that this is what you need