Rafael Pereira
Rafael Pereira

Reputation: 205

Convertir .xml a JSON

I'm loading an Excel file and with the SheetJS library I convert it into a JSON, but I receive it this way:

JSON.parse(output)

data: Array(15)
0: (11) ["id", "created", "device", "plataforma", "locale", "first_render_time", "total_load_time", "total_size", "load_without_js", "request", "score"]
1: (11) ["424", "2019-11-19T23:58:07.977Z", "mobile", "PSI", "es-CO", "9840", "37602", "6286909", "3978", "227", "0"]
2: (11) ["423", "2019-11-19T23:57:43.340Z", "desktop", "PSI", "es-CO", "1981", "7903", "5424580", "3452", "204", "0.19"]
3: (11) ["422", "2019-11-19T23:55:24.163Z", "mobile", "PSI", "es-CO", "10052", "38450", "5850544", "3506", "231"

The first row must be the JSON name.

I want to receive it this way.

[
  {
    id: 6049,
    created: "2019-11-29T20:05:22.870Z",
    device: "desktop",
    plataforma: "PSI",
    locale: "es-CO",
    first_render_time: 2042,
    total_load_time: 7904,
    total_size: 6000627,
    load_without_js: 3722,
    request: 212,
    score: "0.18"
  },
  {
    id: 6046,
    created: "2019-11-29T20:00:23.317Z",
    device: "desktop",
    plataforma: "PSI",
    locale: "es-CO",
    first_render_time: 2052,
    total_load_time: 7974,
    total_size: 6001442,
    load_without_js: 3648,
    request: 212,
    score: "0.18"
  }
]

Upvotes: 1

Views: 73

Answers (2)

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92417

You can convert data like that

data.map((a,i)=> Object.fromEntries(data[0].map((x,j)=> [x,data[i][j]])) ).slice(1)

data = [ ["id", "created", "device", "plataforma", "locale", "first_render_time", "total_load_time", "total_size", "load_without_js", "request", "score"],
["424", "2019-11-19T23:58:07.977Z", "mobile", "PSI", "es-CO", "9840", "37602", "6286909", "3978", "227", "0"],
["423", "2019-11-19T23:57:43.340Z", "desktop", "PSI", "es-CO", "1981", "7903", "5424580", "3452", "204", "0.19"],
["422", "2019-11-19T23:55:24.163Z", "mobile", "PSI", "es-CO", "10052", "38450", "5850544", "3506", "231"]];

result= data.map((a,i)=> Object.fromEntries(data[0].map((x,j)=> [x,data[i][j]])) ).slice(1);

console.log(result);

Upvotes: 1

Ori Drori
Ori Drori

Reputation: 191976

Split the header (1st row) from the body (all other rows) by destructuring the array. Iterate the body using Array.map(), and then reduce the header to an object, and take the data from the current array:

const convertToObjects = ([header, ...body]) =>
  body.map(arr => header.reduce((r, prop, i) => {
    r[prop] = arr[i];
    
    return r;
  }, {}));

const data = [
  ["id", "created", "device", "plataforma", "locale", "first_render_time", "total_load_time", "total_size", "load_without_js", "request", "score"],
  ["424", "2019-11-19T23:58:07.977Z", "mobile", "PSI", "es-CO", "9840", "37602", "6286909", "3978", "227", "0"],
  ["423", "2019-11-19T23:57:43.340Z", "desktop", "PSI", "es-CO", "1981", "7903", "5424580", "3452", "204", "0.19"]
];

const result = convertToObjects(data);

console.log(result);

Upvotes: 1

Related Questions