JSON Formatter

Read a JSON file and print clean formatted output.

Start building, submit solution and get feedback from the community.
2Submit Solution
5 upvotes10 upvotes

JSON Formatter

You are required to build a CLI tool using Node.js that reads a JSON file and prints formatted JSON to the terminal.

The tool should accept a JSON file path from the terminal. If the user does not pass a file path, it should print a friendly error.

Create a file called user.json with this content:

json
{"name":"Ava","role":"Developer","skills":["JavaScript","Node.js"]}

Create another file called broken.json with this content:

json
{"name":"Ava","role":"Developer","skills":["JavaScript","Node.js"]

This file is missing the final }. Keep it broken so you can test the error path.

Here are some example commands you can run to test your CLI:

bash
node json-formatter.js user.jsonnode json-formatter.js broken.jsonnode json-formatter.js missing.jsonnode json-formatter.js

On successful execution, the output should look like this:

json
{  "name": "Ava",  "role": "Developer",  "skills": [    "JavaScript",    "Node.js"  ]}

If the file contains invalid JSON, print a friendly error:

txt
error: invalid JSON in file: broken.json

If the file cannot be read, print this error:

txt
error: could not read file: missing.json

If the user does not pass a file path, print this error:

txt
error: please provide a JSON file path

Errors should go to stderr, and the command should set a non-zero exit code.

The goal of this project is to practice reading files, parsing JSON, handling invalid data, and printing clean output that another command could redirect into a file.

You will need these Node.js APIs:

  • process.argv to read the file path from the terminal.

  • node:fs/promises to read the file contents.

  • JSON.parse() to turn JSON text into a JavaScript value.

  • JSON.stringify() to print formatted JSON.

Solution

Solution:

js
import { readFile } from 'node:fs/promises';const filePath = process.argv[2];function printError(message) {  console.error(`error: ${message}`);  process.exitCode = 1;}function formatJson(text, filePath) {  let value;  try {    value = JSON.parse(text);  } catch {    printError(`invalid JSON in file: ${filePath}`);    return null;  }  return JSON.stringify(value, null, 2);}async function main() {  if (!filePath) {    printError('please provide a JSON file path');    return;  }  let text;  try {    text = await readFile(filePath, 'utf8');  } catch {    printError(`could not read file: ${filePath}`);    return;  }  const formattedJson = formatJson(text, filePath);  if (formattedJson === null) {    return;  }  console.log(formattedJson);}await main();

Join the Community

roadmap.sh is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Rank  out of 28M!

359K

GitHub Stars

Star us on GitHub
Help us reach #1

+90kevery month

+2.8M

Registered Users

Register yourself
Commit to your growth

+2kevery month

49K

Discord Members

Join on Discord
Join the community

RoadmapsGuidesFAQsYouTube

roadmap.shby@nilbuild

Community created roadmaps, best practices, projects, articles, resources and journeys to help you choose your path and grow in your career.

© roadmap.sh·Terms·Privacy·

ThewNewStack

The top DevOps resource for Kubernetes, cloud-native computing, and large-scale development and deployment.