Skip to content
iamnoobs

Developer Tutorials

How to Format JSON: A Practical Guide

Learn what JSON formatting does, how to make JSON easier to read, and how to spot common formatting problems.

By iamnoobs

How to Format JSON: A Practical Guide

JSON is a popular format for exchanging data between applications, APIs, and backend services. However, JSON copied from an API response, configuration file, or log may appear as one long line, making it difficult to read and debug.

Formatting JSON makes the data easier to read by adding indentation, line breaks, and consistent spacing without changing its underlying structure or values.

In this guide, you'll learn what JSON formatting does, how to format JSON using an editor or an online tool, and how to identify common syntax errors. You can also use the IamNoobs JSON Formatter to format and inspect JSON data.

What Is JSON Formatting?

JSON formatting, sometimes called pretty-printing, is the process of arranging JSON data with consistent indentation and line breaks.

A JSON document can be valid even when it is written on a single line. Formatting changes how the document looks, not what the data represents.

For example, a compact JSON object might look like this:

{"name":"Amit","age":30,"skills":["C","Python","JavaScript"]}

The same JSON can be formatted as follows:

{
  "name": "Amit",
  "age": 30,
  "skills": [
    "C",
    "Python",
    "JavaScript"
  ]
}

Both versions represent the same JSON data.

Why is JSON formatting useful?

Formatting JSON is helpful when you need to:

  • Read API responses containing many fields.
  • Debug configuration files and application data.
  • Review JSON during software development.
  • Inspect nested objects and arrays.
  • Compare JSON documents during testing.
  • Understand data exchanged between frontend and backend systems.

For developers working with REST APIs, configuration files, or JavaScript applications, readable JSON can make troubleshooting much easier.

Formatting vs. validation vs. minification

These operations are related, but they serve different purposes.

Operation Purpose Example use
Formatting Makes JSON easier to read Debugging API responses
Validation Checks whether JSON follows the syntax rules Finding missing commas or invalid values
Minification Removes unnecessary whitespace Reducing payload size where appropriate
Sorting keys Reorders object properties Producing consistent output for comparisons

A formatter may also validate JSON as part of its process, but formatting and validation are not identical. Validating JSON does not necessarily mean its values or business logic are correct.

How to Format JSON

There are several ways to format JSON, depending on whether you're working in a browser, a code editor, or a command-line environment.

Method 1: Use an online JSON formatter

An online formatter is convenient when you need to format JSON quickly without installing additional software.

Follow these steps:

  1. Open the IamNoobs JSON Formatter.
  2. Copy your JSON data from an API response, file, or application log.
  3. Paste the JSON into the input field.
  4. Run the formatting operation using the available controls.
  5. Review the formatted output.
  6. Copy the result if it looks correct.

If the formatter reports an error, inspect the indicated position and check for missing commas, quotation marks, brackets, or braces.

The exact controls depend on the current implementation of the tool.

Method 2: Format JSON in Visual Studio Code

If you use Visual Studio Code, you can format JSON directly in the editor.

  1. Open your .json file in Visual Studio Code.
  2. Make sure the file is recognized as JSON.
  3. Open the Command Palette.
  4. Search for Format Document.
  5. Select the command to format the entire file.

The default keyboard shortcuts are:

  • Windows/Linux: Shift + Alt + F
  • macOS: Shift + Option + F

The exact behavior can depend on your editor configuration, installed extensions, and selected formatter.

For example, this unformatted document:

{"server":{"host":"localhost","port":3000},"debug":true}

Becomes:

{
  "server": {
    "host": "localhost",
    "port": 3000
  },
  "debug": true
}

You can also configure the editor's indentation settings to use two or four spaces, depending on your project's conventions.

Method 3: Format JSON using Python

Python's built-in json module can format JSON data without requiring an external package.

To format JSON stored in a Python string:

import json

data = '{"name":"Amit","age":30,"active":true}'

parsed_data = json.loads(data)
formatted_json = json.dumps(
    parsed_data,
    indent=2,
    ensure_ascii=False
)

print(formatted_json)

Output:

{
  "name": "Amit",
  "age": 30,
  "active": true
}

The indent=2 option uses two spaces for each indentation level. The ensure_ascii=False option preserves non-ASCII characters in the output instead of escaping them unnecessarily.

To format a JSON file, you can use:

import json

with open("input.json", "r", encoding="utf-8") as file:
    data = json.load(file)

with open("formatted.json", "w", encoding="utf-8") as file:
    json.dump(
        data,
        file,
        indent=2,
        ensure_ascii=False
    )
    file.write("\n")

This script reads input.json and writes the formatted result to formatted.json.

The output file is separate from the original, so you can inspect the result before replacing the source file.

Method 4: Format JSON using the command line

If Node.js is installed, you can use its built-in JavaScript runtime to format a JSON file.

Run this command in your terminal:

node -e "console.log(JSON.stringify(require('./input.json'), null, 2))"

The command reads input.json, parses it, and prints the formatted JSON with two spaces of indentation.

To save the output to a new file:

node -e "console.log(JSON.stringify(require('./input.json'), null, 2))" > formatted.json

This method works for valid JSON files. If the input contains syntax errors, parsing fails rather than producing a formatted document.

Example: Before and After

Let's look at a practical example of formatting a JSON response containing user information.

Before formatting

The following JSON is compact and valid, but it is difficult to inspect when the document becomes larger.

{"users":[{"id":1,"name":"Amit","active":true},{"id":2,"name":"Priya","active":false}],"total":2}

After formatting

The formatted version separates objects, properties, and arrays onto readable lines.

{
  "users": [
    {
      "id": 1,
      "name": "Amit",
      "active": true
    },
    {
      "id": 2,
      "name": "Priya",
      "active": false
    }
  ],
  "total": 2
}

What changed?

The formatted JSON uses:

  • Indentation to show nested structures.
  • Line breaks to separate properties and records.
  • Consistent spacing after colons and between values.
  • Visual grouping to make arrays and objects easier to inspect.

The property names, values, data types, and overall structure remain unchanged.

Understanding indentation

Indentation shows how deeply a value is nested inside an object or array.

Consider this JSON:

{
  "application": {
    "name": "IamNoobs",
    "settings": {
      "theme": "dark",
      "language": "en"
    }
  }
}

The application object contains name and settings. The settings object contains theme and language.

Each additional indentation level makes the relationship between parent and child structures easier to see.

Two spaces vs. four spaces

Both styles are valid JSON formatting conventions.

Two-space indentation:

{
  "name": "IamNoobs",
  "settings": {
    "theme": "dark"
  }
}

Four-space indentation:

{
    "name": "IamNoobs",
    "settings": {
        "theme": "dark"
    }
}

Choose the style used by your project or team. Indentation width does not change the meaning of the JSON.

Common JSON Formatting Problems

A formatter can usually format valid JSON, but malformed input must be corrected first. The following issues commonly occur when copying JSON from APIs, documentation, configuration files, or chat messages.

1. Missing commas

JSON uses commas to separate properties within an object and elements within an array.

Invalid JSON:

{
  "name": "Amit"
  "age": 30
}

There is no comma between the name and age properties.

Correct JSON:

{
  "name": "Amit",
  "age": 30
}

How to fix it: Check the separator between each property or array element.

2. Trailing commas

Standard JSON does not allow a trailing comma after the last property or array element.

Invalid JSON:

{
  "name": "Amit",
  "age": 30,
}

Correct JSON:

{
  "name": "Amit",
  "age": 30
}

Some programming languages and configuration formats permit trailing commas, but standard JSON does not.

3. Single quotes instead of double quotes

JSON requires double quotes around property names and string values.

Invalid JSON:

{
  'name': 'Amit'
}

Correct JSON:

{
  "name": "Amit"
}

This distinction matters when copying object literals from JavaScript code into a JSON file.

4. Unquoted property names

JSON property names must be strings enclosed in double quotes.

Invalid JSON:

{
  name: "Amit",
  age: 30
}

Correct JSON:

{
  "name": "Amit",
  "age": 30
}

JavaScript allows unquoted property names in many object literals, but JSON does not.

5. Comments inside JSON

Standard JSON does not support // or /* ... */ comments.

Invalid JSON:

{
  // Application name
  "name": "IamNoobs"
}

Correct JSON:

{
  "name": "IamNoobs"
}

If you need comments in a configuration file, consider a format that explicitly supports them, or store explanatory documentation separately.

6. Missing closing brackets or braces

Every JSON object must have a matching closing brace, and every JSON array must have a matching closing bracket.

Invalid JSON:

{
  "users": [
    {
      "name": "Amit"
    }
}

The array is not closed correctly.

Correct JSON:

{
  "users": [
    {
      "name": "Amit"
    }
  ]
}

How to fix it: Check the opening and closing delimiters, particularly in deeply nested data.

7. Invalid values such as undefined and NaN

JSON supports strings, numbers, objects, arrays, booleans, and null. It does not support JavaScript values such as undefined, NaN, or Infinity as JSON values.

Invalid JSON:

{
  "name": undefined,
  "score": NaN
}

A valid representation might be:

{
  "name": null,
  "score": null
}

Use null only when it accurately represents the intended meaning. Otherwise, choose an appropriate valid value or omit the property when that matches your data model.

8. Incorrectly escaped quotation marks

Quotation marks inside a JSON string must be escaped.

Invalid JSON:

{
  "message": "He said "Hello""
}

Correct JSON:

{
  "message": "He said \"Hello\""
}

A formatter cannot reliably correct every malformed string automatically, so inspect the reported error location.

9. Copying extra text around the JSON

When copying an API response or code example, you may accidentally include Markdown fences, explanatory text, or terminal output.

For example, this is not a standalone JSON document:

Here is the response:
{"status":"success"}

The JSON content itself is valid, but the leading sentence is not part of JSON syntax.

How to fix it: Copy only the JSON document, excluding explanatory text and Markdown code fences.

10. Valid JSON with incorrect data

Formatting and syntax validation cannot confirm that your data is logically correct.

For example:

{
  "quantity": -5,
  "price": 100
}

This is syntactically valid JSON, even if a negative quantity is not allowed in your application.

After formatting, validate your data against the application's requirements or a JSON Schema when appropriate.

Format JSON with the IamNoobs Tool

The IamNoobs JSON Formatter provides a convenient way to make JSON data easier to read without manually adjusting indentation and line breaks.

It can be useful when inspecting API responses, debugging application data, reviewing configuration files, or preparing JSON for documentation.

Example: Format an API response

Suppose you receive this compact response:

{"status":"success","data":{"userId":101,"name":"Amit","roles":["developer","tester"]}}

The expected formatted structure is:

{
  "status": "success",
  "data": {
    "userId": 101,
    "name": "Amit",
    "roles": [
      "developer",
      "tester"
    ]
  }
}

Steps to use the formatter

  1. Open the IamNoobs JSON Formatter.
  2. Paste your JSON into the input area.
  3. Run the formatting operation using the available controls.
  4. Review the formatted output.
  5. If an error appears, correct the input and try again.
  6. Copy the result using the available controls.

The exact interface and options depend on the current implementation of the tool.

Checklist before using formatted JSON

  • Syntax: Confirm that the JSON parses successfully.
  • Structure: Check that objects and arrays are nested correctly.
  • Values: Verify that the original property values remain unchanged.
  • Data types: Make sure strings, numbers, booleans, and null are represented correctly.
  • Indentation: Use a consistent style.
  • Sensitive data: Avoid submitting confidential information to an online service unless its data-handling practices meet your requirements.

Formatting should improve readability without unintentionally changing your data.

Frequently Asked Questions

1. What does formatting JSON mean?

Formatting JSON means arranging its content using indentation, line breaks, and consistent spacing. This makes objects, arrays, and nested properties easier to read while preserving the JSON data structure.

2. How do I format JSON online for free?

Open an online JSON formatter, paste your JSON into the input field, and run the formatting operation. You can try the IamNoobs JSON Formatter to format compatible JSON data in your browser.

3. Does formatting JSON change its data?

A standard formatter parses the JSON and produces a more readable representation without intentionally changing its values or structure. However, formatting can change the textual representation, whitespace, and property ordering depending on the implementation. Review the output when exact textual preservation matters.

4. What indentation should I use for JSON?

Two spaces and four spaces are both common choices. Neither is required by the JSON specification. Use a consistent style that matches your project or team's conventions.

5. Why can't my JSON formatter process the input?

The input may contain invalid syntax, such as missing commas, trailing commas, single quotes, unquoted keys, comments, or unmatched brackets. Correct the reported error before formatting.

6. What is the difference between JSON formatting and validation?

Formatting improves readability by changing whitespace and layout. Validation checks whether the input follows JSON syntax rules. A tool may perform both operations, but valid JSON is not necessarily correct according to your application's business rules.

7. What is the difference between formatting and minifying JSON?

Formatting adds whitespace and indentation to make JSON easier to read. Minification removes unnecessary whitespace to produce a more compact representation. Both can preserve the underlying JSON values and structure.

8. Can I format a large JSON file?

Yes, but very large files may require more memory and processing time, especially in browser-based tools. For large datasets, consider a local editor, command-line utility, or streaming workflow that can handle the file efficiently.

9. Can I format JSON in Visual Studio Code?

Yes. Open the JSON file and use the Format Document command from the Command Palette. The default shortcut is Shift + Option + F on macOS and Shift + Alt + F on Windows or Linux.

10. Is online JSON formatting safe?

It depends on how the service processes and stores your input. For sensitive information such as credentials, personal records, or private API responses, verify the tool's privacy practices or use a local formatter.

Conclusion

JSON formatting makes structured data easier to read, debug, and maintain. Whether you are working with API responses, configuration files, or application logs, consistent indentation helps you understand nested objects and arrays quickly.

Remember that formatting does not replace syntax validation or application-level checks. If your JSON is invalid, correct the syntax first; if it is valid but incorrect, verify the data against your application's requirements.

For a quick and convenient workflow, try the IamNoobs JSON Formatter to make your JSON easier to inspect.