The problem of converting dynamic objects to the specified Json format

problem description

after the dynamic object is obtained and converted to Json using the following code

dynamic realTimeData = db.Database.DynamicSqlQuery("exec QueryRealTimeData @treeId", new SqlParameter("@treeId", treeId)) ;
 
int draw = Request["draw"] != null ? int.Parse(Request["draw"]) : 1;
 
var jsonDataTemp = new {
    data = realTimeData,
    draw = draw
};
 
return Json(jsonDataTemp, JsonRequestBehavior.AllowGet);

found that the converted Json format (format 1 below) is not the format I need:

{
    "data": [{
        "B01-Rtd": 10.285,
        "001-Rtd": 7.522,
        "011-Rtd": 20.903
    }, {
        "B01-Rtd": 10.031,
        "001-Rtd": 7.518,
        "011-Rtd": 20.903
    }],
    "draw": 1
}

I actually need to convert it to the following format (format 2) for the data source of the JQuery DataTables plug-in:

[{
    "COLUMNS": [
        {"title": "B01-Rtd"}, 
        {"title": "001-Rtd"}, 
        {"title": "011-Rtd"}
        ],
    "DATA": [
        ["10.285", "7.522", "20.903"],
        ["10.031", "7.518", "20.903"]
    ],
    "draw": 1
}]

what result do you expect?

could you tell me how to convert the data into format 2, or how to use the data in format 1 for the data source of DataTables?

Note that this is a dynamically generated anonymous object. I don"t know which columns will be there in advance, so I can"t initialize the columns explicitly. I need to dynamically generate a Json, similar to format 2, and then initialize the DataTables with something like the following JS

$("-sharpexample").dataTable({
            "data": dataObject[0].DATA,
            "columns": dataObject[0].COLUMNS
        });
Mar.26,2021

format one is converted to format two:

let data = {
  "data": [{
    "B01-Rtd": 10.285,
    "001-Rtd": 7.522,
    "011-Rtd": 20.903
  }, {
    "B01-Rtd": 10.031,
    "001-Rtd": 7.518,
    "011-Rtd": 20.903
  }],
  "draw": 1
}

function trans(source) {
  let columns = []
  let data = []
  let keys = []
  source.data.forEach((cur, i) => {
    if (i === 0) {
      keys = Object.keys(cur)
      columns = keys.map(key => ({title: key}))
    }
    data.push(keys.map(key => cur[key]))
  })
  return [{
    COLUMNS: columns,
    DATA: data,
    draw: source.draw
  }]
}
trans(data)
Menu