1

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
  ]
]
nb_nb_nb
  • 865
  • 4
  • 15

3 Answers3

1

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.values(structure.data[0]).map(v => typeof v)));
Đinh Carabus
  • 2,443
  • 3
  • 17
  • 34
1

Do a map() through Object.values(obj) and use typeof value to return the type

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("Keys are ..");
 console.log(ele.map( structure =>  Object.keys(structure.data[0])))
console.log("and their correspondent value types are ..."); 
 console.log(ele.map( structure =>  Object.values(structure.data[0]).map(value => typeof value)))
.as-console-wrapper { max-height: 100% !important; top: 0; }
M A Salman
  • 3,222
  • 2
  • 10
  • 24
0

Javascript is a prototype language, so you can use the constructor.name property on any structure in js to see what type it is.

simple example:

const a = { t: 5, s: '', d: false, e:[], f:{}, d:5.53 };
for (let k in a) {
  console.log(k,a[k].constructor.name);
}
mplungjan
  • 134,906
  • 25
  • 152
  • 209
r3wt
  • 4,262
  • 2
  • 28
  • 52