title: Check POST body request syntax and format with Node.js Express
body: <p>I would like to simply check the syntax of POST request body that my server gets on the route called '/route' :</p>
  
  <ul>
  <li>check if the body's format is actually JSON,</li>
  <li>check if the body's syntax is correct (no missing brackets or missing quotes).</li>
  </ul>
  
  <p>/// My code ///</p>
  
  <pre><code>app.use(express.json());
  
  function isJsonString(str) {
      try {
          console.log('JSON Parsing function...');
          JSON.parse(str);
      } 
      catch (e) {
          console.log("Error : " + e);
          return false;
      }
      return true;
  }
  
  app.post('/route', function(req, res) {
  
      var isJsonString_result = isJsonString(req.body);
      console.log(isJsonString_result);
  
      if(isJsonString_result === true){
          console.log('OK continue');
          res.status(200).send('ok');
      }
      else{
          console.log('Wrong body format');
          res.status(404).send('ko');
      }
  })
  </code></pre>
  
  <p>/// The results ///</p>
  
  <p>Here are the results I get when I send a POST request from Postman with this JSON (sent with header "Content-Type": "application/json") :</p>
  
  <pre><code>{
    "key1": "value1",
    "key2": "value2"
  }
  </code></pre>
  
  <p>/// Postman result ///</p>
  
  <p>ko</p>
  
  <p>/// Console.log ///</p>
  
  <p>JSON Parsing function...</p>
  
  <blockquote>
    <p>Error : SyntaxError: Unexpected token o in JSON at position 1 false</p>
  </blockquote>
  
  <p>Wrong body format</p>
  
  <p>=> the explanation seems to be that "JSON.parse" can not parse existing JSON...</p>
  
  <p>So first question : how can I check if the body is correct JSON format please ?</p>
  
  <p>and with this other JSON with missing quotes (always sent with header "Content-Type": "application/json") :</p>
  
  <pre><code>{
    "key1": "value1",
    "key2: "value2"
  }
  </code></pre>
  
  <p>/// Postman result ///</p>
  
  <p>Long "Error" message begining with "SyntaxError: Unexpected token v in JSON at position 32"</p>
  
  <p>/// Console.log ///</p>
  
  <p>The same :</p>
  
  <blockquote>
    <p>SyntaxError: Unexpected token v in JSON at position 32 at JSON.parse
    ()</p>
  </blockquote>
  
  <p>...
  ...</p>
  
  <p>=> No idea how to handle this issue, if there are many POST requests with wrong body syntax, my "log" can potentially grow very fast.</p>
  
  <p>Can you help me please with both questions ?</p>
  
  <p>Thank you !</p>