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

149
Vistas
JSON Stringify doesnt format the same into component

I have an array that I need to format using JSON Stringify. I am using the Chakra UI component to display the text but it doesnt show correctly in the component. In the console it is showing how I want it however. This is the formatted text:

[
  {
    "price": "15.99",
    "size": "m"
  },
  {
    "price": "2.99",
    "size": "s"
  }
]

This is how I use it in the component:

<Code children={JSON.stringify(data, null, 2)} />

But running the app in the component it shows:

[ { "price": "15.99", "size": "m" }, { "price": "2.99", "size": "s" } ]

With no line breaks. Ive tried wrapping the JSON.stringify term in a p tag but didnt work. Not sure if there is a trick to this or a property in the code css that cuts off the lines

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

0

Your use of JSON.stringify() is correct. Yet you have to set the container to display those linebreaks.

const data = [
  {
    "price": "15.99",
    "size": "m"
  },
  {
    "price": "2.99",
    "size": "s"
  }
]

document.querySelector('p').textContent = JSON.stringify(data, null, 2)
p{
  white-space: pre
}
<p>
</p>

about 4 years ago · Juan Pablo Isaza Denunciar

0

As @CherryDT commented, you just need to use the correct element to display the JSON output, and <pre> is best suited, unless you use CSS to alter the behaviour of another element.

const data = [
  {
    "price": "15.99",
    "size": "m"
  },
  {
    "price": "2.99",
    "size": "s"
  }
];

const element = document.getElementById(`app`);

element.innerHTML = JSON.stringify(data, null, 2);
<pre id="app"></pre>

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