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

377
Vistas
How to fix unexpected end of json error when i run JSON.parse()

I'm working on a document verification project. my code works when the document exists or when the logged person issued the document. but it fails and returns an "unexpected end of JSON" error when the document is not issued by the logged person(for example for new user). the error is returned when I used JSON.parse(documents) on Ejs. how can I catch the error with try-catch or any other means in EJS? here is the result of code when the document found(expected output)

<% console.log('fit', JSON.parse(documents, null, '\t')) %>

Output
fit [ { Key: 'DOCUMENT0', Record: { name: 'bachelor of science degree', url: 'https://bitcoin.org/ggg.pdf', issuedBy: 'sol123', dateOfIssuance: '12:18 PM, 25 September, 2021', hashedDoc: 'dac729a8acf4b8a88f73f5bd84206c34e01e0992efa251b772f68696e2c2539c9ed0090e73ef6b87dc24e3177c6fd5341c3e9e24ef14267ce07ab9428aeed897', docType: 'Whitepaper' } } ]

The problem is when the document is not found(for example, if the user is new and not yet issued document);

SyntaxError: /home/verification/fabcar/javascript/views/pages/dashboard.ejs:15
    13|     <div class="list-group" id="list-tab" role="tablist" style="margin-top: 10px;">
    14| 
 >> 15|     <% JSON.parse(documents).forEach(document => { %>
    16|         <a class="list-group-item list-group-item-action" id="<%= document.Key %>" data-bs-toggle="tab"
    17|           href="#list-<%= document.Key %>" role="tab" aria-controls="list-<%= document.Key %>" style="min-width: 200px;"><%= document.Key %></a>
    18|     <% }) %>

Unexpected end of JSON input
    at JSON.parse (<anonymous>

what I want is, how can I display the NO DOCUMENT ISSUED message on EJS when there is no document issued by the user. please I need your help I'm new to javaScript and ejs.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

you can use try catch

try {

  let user = JSON.parse(json); 

} catch (err) {
  
  alert( "user is new and not yet issued document" );
  console.log( err.name );
  console.log( err.message );
}

by doing so you can verify each error and respond accordingly.

or else use validation true or false function like following and continue accordingly

function isValidJSON(text) {
  try {
    JSON.parse(text);
    return true;
  } catch {
    return false;
  }
}

Updated for the question : How to create try catch in ejs

here is example of try catch

<%

function isValidJSON(text) {
  try {
    JSON.parse(text);
    return true;
  } catch {
    return false;
  }
}
var json1 = '{"item" : {"item2" : "item Data"}}';
var json2 = '{"item" : {"item2" : "item Data}}';

%>



pasing json 
<% if(isValidJSON(json1))  { %>
<p>Valid jsonr!</p>
<% } else { %>
<p>Invalid Json!</p>

<% } %>

pasing json 
<% if(isValidJSON(json2))  { %>
<p>Valid jsonr!</p>
<% } else { %>
<p>Invalid Json!</p>

<% } %>

you can see the json1 is valid and json2 is invalid.

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