Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
Produce CSV from JSON object

I have a JSON Object like so that I am trying to produce a CSV from

{
    "football": {
        "count": 2109,
        "emailed": 0,
        "optin": 2109,
    },
    "tennis": {
        "count": 61114,
        "emailed": 11111,
        "optin": 555,
    }
}

However, the fields titles have to be different

What I am after is essentially something like this

Sport, Total, Emails Sent, No Opt Ins,
football, 2109, 0, 2109,
tennis, 61114, 11111, 555

I am tried using packages but can't seem to get it working, as such, then tried doing it myself

const fields = ['Sport', 'Total', 'Emails Sent', 'No Opt Ins'];

I wrapped my JSON in an array so I could use map

try {
  const replacer = (key, value) => (value === null ? '' : value);
  const csv = [
    fields.join(','),
    ...dataReduced.map((row) => fields.map((fieldName) => JSON.stringify(row[fieldName], replacer)).join(',')),
  ].join('\r\n');
  console.log(csv);
} catch (err) {
  console.error(err);
}

Obviously that wont work because the field names do not match the Object names. How can I produce a CSV output in the way described above?

Thanks

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Since the row is an object literal, the keys may not necessarily be sorted, so the map here is not enough, there should be a mapper for columns, something like this:

const row = {
    "football": {
        "count": 2109,
        "emailed": 0,
        "optin": 2109,
    },
    "tennis": {
        "count": 61114,
        "emailed": 11111,
        "optin": 555,
    }
};

const head = {
  football: {id: 0, name: 'Sport'},
  count: {id: 1, name: 'Total'},
  emailed: {id: 2, name: 'Emails Sent'},
  optin: {id: 3, name: 'No Opt Ins'}
};

const csv = [Object.values(head).sort((a, b) => a.id - b.id)
  .map(value => value.name).join()];

Object.entries(row).forEach(([key, value]) => {
  const line = [key];
  Object.entries(value).forEach(([key, value]) => {
    line[head[key].id] = value;
  });
  csv.push(line.join());
});

console.log(csv.join('\n'));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda