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

261
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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