I have a JSON file and with the help of @Supercool was able to get key names for each. I wanted to be able to get the type of values.
JSON Structure:
[
{
"id": 1536700,
"title": "final_output",
"error": "",
"data": [
{
"metric": 4940616.0,
"title": "d_revenue"
},
{
"metric": 5132162.0,
"title": "p_revenue"
},
{
"metric": 4954576.0,
"title": "s_revenue"
},
{
"metric": 4882217.0,
"title": "u_revenue"
},
{
"metric": 4869609.0,
"title": "t_revenue"
},
{
"metric": 5075422.0,
"title": "w_revenue"
},
{
"metric": 4461996.0,
"title": "v_revenue"
}
]
}
]
Next Structure:
[
{
"run_id": 1536700,
"code_title": "select_data",
"error": "",
"data": [
{
"user_name": "C_51",
"num1": 51,
"num2": 101,
"num3": 151
},
{
"user_name": "H_51",
"num1": 51,
"num2": 101,
"num3": 151
},
{
"user_name": "C_52",
"num1": 52,
"num2": 102,
"num3": 152
},
{
"user_name": "H_52",
"num1": 52,
"num2": 102,
"num3": 152
},
{
"user_name": "C_53",
"num1": 53,
"num2": 103,
"num3": 153
}
]
}
]
Use Object.keys(yourObject) to get the keys
$.getJSON(api, function(elem) {
let keys=elem.map( structure => Object.keys(structure.data[0]))
});
Run the following snippet to check if it works
let ele= [{"id": 1536700,"title": "final_output","error": "",
"data": [{"metric": 4940616.0,"title": "d_revenue"},{"metric": 5132162.0,"title": "p_revenue"},{"metric":4954576.0,"title": "s_revenue"},{"metric": 4882217.0,"title":"u_revenue"},{"metric": 4869609.0,"title":"t_revenue"},{"metric": 5075422.0,"title": "w_revenue"},{"metric": 4461996.0,"title": "v_revenue"}
]
},
{"run_id": 1536700,"code_title": "select_data","error": "",
"data": [{"user_name": "C_51","num1": 51,"num2": 101,"num3": 151},{"user_name": "H_51","num1": 51,"num2": 101, "num3": 151},{"user_name": "C_52","num1": 52,"num2": 102,"num3": 152},{"user_name": "H_52","num1": 52,"num2": 102,"num3":152},{"user_name": "C_53","num1": 53,"num2": 103,"num3": 153}
]}]
console.log(ele.map( structure => Object.keys(structure.data[0])))
console.log(ele.map( structure => Object.values(structure.data[0])))
.as-console-wrapper { max-height: 100% !important; top: 0; }
I want to be able to get the type of each of the values. Is it possible for me to get this for the values listed above?
[
[
number,
string
],
[
string,
number,
number,
number
]
]