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

78
Visualizações
Trouble when parsing JSON string

I'm dumping JSON in Django view and then parsing JSON in JS to get the data.

My view.py (Django)

ibms = []
for i in range(2, 5):
    ibm = Mapa(i, wsMapa)
    ibms.append(ibm.__dict__)
ibms = json.dumps(ibms)

return render(request, 'mapas/index.html', {'ibms': ibms})

The ibm variable output in Django template is:

[{"numeroIbm": "AUTO P"}, {"numeroIbm": "PTB"}, {"numeroIbm": "FAROL"}]

My index.html (JS inside)

{{ ibms|json_script:"ibms" }}
<script>
    const mydata = JSON.parse(document.getElementById("ibms").textContent);
    const mydata2 = JSON.parse(mydata);
</script>

The issue is: I'm having to JSON.parse double times to get the JS object. The variable mydata, despite the JSON.parse, is string typeof. I only get the final result when I JSON.parse for the second time (mydata2).

What is happening, pls?

Tks in advance!

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

0

You should not dump it in the view, so:

ibms = [Mapa(i, wsMapa).__dict__ for i in range(2, 5)]

return render(request, 'mapas/index.html', {'ibms': ibms})

and thus parse it as:

{{ ibms|json_script:"ibms" }}
<script>
    const mydata = JSON.parse(document.getElementById("ibms").textContent);
    // no second parse
</script>
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