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

235
Visualizações
Why JSON.stringify() don't work correctly

I'm actually creating a JSON file with nodejs but when i use the JSON.stringify. My Json Object work correctly when i print it :

My code

But when i stringify my json object i get that :

What i receive when i print my json object

A big part of my json information don't want to be save i don't know why Here's the code :

What i receive when i print my stringified json object

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are attempting to use a 'sparse array' to store your data. Arrays do not have a clear way to be instantiated with elements at a specified index.

An object (or a Map) would suit your requirements better. To use an object, I modified your 'accounts' initation code that was commented:

// replace this:
dataObj["accounts"] = [];
// with this:
dataObj["accounts"] = {};

The other code you are using (e.g. accessing elements using the square brackets) will work the same. You should probably make the same adjustments to the other arrays you are using.

Sparse arrays can get ugly in some notations if you have large blank spaces (which your UUID methodology would certainly have). An object or a Map will not suffer that shortcoming.

An example of similar code, using objects instead of arrays:

let dataObj = {};
dataObj["accounts"] = {};
let uuid = 'abcdefghij12344';
dataObj["accounts"][uuid] = {}
dataObj["accounts"][uuid]['active'] = true;
dataObj["accounts"][uuid]['someotherval'] = true;
 
console.log(dataObj);
//outputs:

{ accounts: { abcdefghij12344: { active: true, someotherval: true } } }

let stringified = JSON.stringify(dataObj);
console.log(stringified);

// Outputs:
{"accounts":{"abcdefghij12344":{"active":true,"someotherval":true}}}

let parsed = JSON.parse(stringified);
console.log(parsed);

//Outputs:
{ accounts: { abcdefghij12344: { active: true, someotherval: true } } }
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