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

158
Vistas
json.stringify object and rename variables in json

I want to serialize an object o, which has a method called, let's say, a. The object also holds a variable, which name is _a.

I now want to parse this object to a JSON string. But the JSON looks something like this:

{
    "_a": "",
    ...
}

Question

Is there a way, to comfortably remove/ replace the _ character(s) (or any character(s)).

What I have tried

  1. The rename parameter of the JSON.stringify() method.
    1.1. Didn't work, because you can only return altered values and no keys.
  2. Iterating all keys of an object, deleting them and creating a new renamed key, and assigning the value (see code below).
    2.1. This works, but is not really readable and nasty, when having "sub-object".
Object.keys(o).forEach(key => {
    Object.defineProperty(o, key.replace("_", ""),
        Object.getOwnPropertyDescriptor(o, key));
    delete o[key];
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Would something like this work for you:

let obj = {
  "_a": "",
  "b": "Test"
}

let result = Object.entries(obj).reduce((acc, [key, value]) => {
  let newKey = key.replace('_', '')
  acc[newKey] = value
  return acc
}, {})

console.log(result)

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