Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda