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

172
Vistas
Escape json using \" in javascript

I have this input {"a":["b","c","d"]}

For whatever reason, I need output like this "{\"a\":[\"b\",\"c\",\"d\"]}"

Instead of this (using JSON.stringify()) '{"a":["b","c","d"]}'

I know I can write some replacements or something. Is there any native Javascript method to do this?

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

0

The problem with using a custom replace is that you risk making the string invalid JSON. Instead you can simply stringify it twice, which will properly escape all special characters such that it can be reliably parsed back to a valid javascript object.

const input = { 'a': ['A string with "quotes"', "c", "d"] };

const string = JSON.stringify(JSON.stringify(input));
console.log(string);

const parsed = JSON.parse(JSON.parse(string));
console.log(parsed);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this one

const a = {"a":["b","c","d"]}
const output = JSON.stringify(a).replaceAll('"', '\\"')
console.log(output)

Hope this helps

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