Get Key names and Values names from JSON and use in Highcharts

noob :

I have a JSON file with the following structure:

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
            }
        ]
    }
]

I want to get the keys inside data and then decide what the x-axis would be and what the y-axis would be and I wanted to do something like http://jsfiddle.net/k32a59vL/1/.

THis is my code:

    $.getJSON(api, function(elem) {
        console.log(elem);
        elem.forEach(d => {

        });
    });

The console.log(elem) gives me the json structure mentioned above. How do I just get metric and title from the first structure and user_name, num1, num2, num3. How do I get those values?

Supercool. :

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])))
.as-console-wrapper { max-height: 100% !important; top: 0; }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=391124&siteId=1