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

258
Visualizações
How to parse JSON from input field

I am trying to parse the json data from an input field on my node.js server. If I simply have the value 16 as a value in the input field, I can simply read it out with request.body.id (<input type="hidden" id="id" name="id" value="16"/>). But when I stringify a json as a value of the input field and parse the data of json, I always get an error SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>). But I can see in the browser console my json data <input type="hidden" id="id" name="id" value="{" a":0.3,"b":0.5,"c":1.3}"> Why can't I read the value of the input field with request.body.id?

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

0

You need to send only the JSON data to the server while calling the respective endpoint in the front-end application. You should not send the html directly to the node js end-point

about 4 years ago · Juan Pablo Isaza Relatório

0

The tag

<input type="hidden" id="id" name="id" value="{" a":0.3,"b":0.5,"c":1.3}">

contains the attributes

type="hidden"
id="id"
name="id"
value="{"

and some garbage

a":0.3,"b":0.5,"c":1.3}"

because the first quote in your JSON closes the attribute.

You have to masquerade the quotes

<input type="hidden" id="id" name="id" value="{&quot; a&quot;:0.3,&quot;b&quot;:0.5,&quot;c&quot;:1.3}">

After clarification in the comments here is a working example:

const json = JSON.stringify({" a":0.3,"b":0.5,"c":1.3});

document.body.innerHTML = `<input value="${json.replaceAll('"', '&quot;')}">`;
const input = document.querySelector('input');

console.log(JSON.parse(input.value));

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