Tips: Parsing JSON with Node.js

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


If you have JSON data as part of a string, the best way to parse it is by using the JSON.parse method that’s part of the JavaScript standard since ECMAScript 5, and it’s provided by V8, the JavaScript engine that powers Node.js.

Example:

const data = '{ "name": "Flavio", "age": 35 }'
try {
  const user = JSON.parse(data)
} catch(err) {
  console.error(err)
}

Note that JSON.parse is synchronous, so the more the JSON file is big, the more time your program execution will be blocked until the JSON is finished parsing.

You can process the JSON asynchronously by wrapping it in a promise and a setTimeout call, which makes sure parsing takes place in the next iteration of the event loop:

const parseJsonAsync = (jsonString) => {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(JSON.parse(jsonString))
    })
  })
}

const data = '{ "name": "Flavio", "age": 35 }'
parseJsonAsync(data).then(jsonData => console.log(jsonData))

If your JSON is in a file instead, you first have to read it.

A very simple way to do so is to use require():

const data = require('./file.json')

Since you used the .json extension, require() is smart enough to understand that, and parse the JSON in the data object.

One caveat is that file reading is synchronous. Plus, the result of the require() call is cached, so if you call it again because you updated the file, you won’t get the new contents until the program exits.

This feature was provided to use a JSON file for the app configuration, and it’s a perfectly valid use case.

You can also read the file manually, using fs.readFileSync:

const fs = require('fs')
const fileContents = fs.readFileSync('./file.json', 'utf8')

try {
  const data = JSON.parse(fileContents)
} catch(err) {
  console.error(err)
}

This reads the file synchronously.

You can also read the file asynchronously using fs.readFile, and this is the best option. In this case, the file content is provided as a callback, and inside the callback you can process the JSON:

const fs = require('fs')

fs.readFile('/path/to/file.json', 'utf8', (err, fileContents) => {
  if (err) {
    console.error(err)
    return
  }
  try {
    const data = JSON.parse(fileContents)
  } catch(err) {
    console.error(err)
  }
})

Lessons in this unit:

0: Introduction
1: Axios crashes the Node.js process when the request fails
2: How to set up a cron job that runs a Node.js app
3: How to get both parsed body and raw body in Express
4: Interact with the Google Analytics API using Node.js
5: How to bulk convert file names using Node.js
6: How to deep copy JavaScript objects using structuredClone
7: How to handle file uploads in Node.js
8: How to send an email using nodemailer
9: Logging all the requests coming through an Express app
10: How to upload an image to S3 using Node.js
11: How to read a CSV file with Node.js
12: How to set the current working directory of a Node.js program
13: How to upload files to S3 from Node.js
14: How to write a CSV file with Node.js
15: Where to host a Node.js app
16: ▶︎ Parsing JSON with Node.js
17: nodemailer, how to embed an image into an email
18: The Pug Guide
19: Restarting a Node process without file changes
20: How to use Sequelize to interact with PostgreSQL