I am trying to extract data from my Json file, however, I cannot use a specific 'key' because it changes on a daily basis.
https://i.sstatic.net/sZySk.png
My attempted solution is as follows:
template: function(params) {
const objects = JSON.parse(JSON.stringify(params.data.masterdetail));
for (const obj of objects) {
const keys = Object.keys(obj);
const cont = 0;
keys.forEach(key => {
const valor = obj[key];
console.log('value ', valor[0]);
});
}
Initially, I tried using index 0 and then introduced 'cont', but with 0
console.log (value is undefined)....
If I instead use
console.log ('value' , valor['name'])
IT WORKS! But I cannot rely on specific keys, and if I use valor[0] it returns as undefined...........
Here is an example of the Json structure:
{
"headers": [
"headerName": "asdasd",
], //end headers
"datas": [
"idaam": "11",
"idorigen": "11",
"masterdetail": [{
"child1": {
"name": "I AM",
"age": "1"
},
"child2": {
"name": "YOU ARE",
"age": "2"
},
"child3": {
"name": "HE IS",
"age": "3"
},
}] //end masterdetail
]//end datas
}//end JSON
Edit :
I am unable to rely on 'keys' since the data fields may change from "name", "typeval" today to "surname", "id" tomorrow.
In the image you can see "4" bits of data.
1º obj[key]{
name = "adopt",
typeval= "",
etc
}
2º obj[key]{
"link" = "enlace",
"map" = "map"
etc
}
If I try this code: I can obtain "name" correctly but
It is NOT ALLOWED to use value['name']
or value[typeval]
since the Json structure is always dynamic.
var objects = params.data.masterdetail[0];
const keys = Object.keys(objects);
let value;
keys.forEach(key => {
value = objects[key];
console.log(value['name']);
console.log(value['typeval']);
});
What I need, for example, is:
var objects = params.data.masterdetail[0];
const keys = Object.keys(objects);
cont = 0 ;
keys.forEach(key => {
value = objects[key];
console.log(value[0]);
});
However, value[0]
leads to undefined results which complicates tracking 'link' in the 2nd object when the index might be different, like 4...
Apologies for any language barriers...