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

379
Visualizações
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 Respostas
Responde à pergunta

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