Code
How to Format JSON: A Practical Guide
Learn how to format JSON, make minified JSON readable, fix common formatting problems and work with structured JSON more efficiently.
What Is JSON Formatting?
JSON formatting is the process of arranging JSON data with consistent indentation, line breaks and spacing so that its structure is easier to read.
Formatted JSON is especially useful when inspecting API responses, configuration files, application data and other structured information.
What Does Formatted JSON Look Like?
Formatted JSON places objects, arrays, properties and values on separate lines where appropriate. Indentation makes it easier to see which properties belong to each object or which values belong to an array.
For example, a compact JSON object can be expanded into multiple lines without changing the underlying data.
What Is Minified JSON?
Minified JSON removes unnecessary whitespace and line breaks so the data occupies less space. It is commonly encountered in network responses, generated files and machine-oriented data.
Minified JSON is valid JSON when its syntax is correct, but it can be difficult for a person to inspect manually. Formatting can expand it into a more readable structure.
Formatted JSON vs Minified JSON
Formatted JSON prioritizes human readability, while minified JSON prioritizes compact representation. Both can contain the same objects, arrays, properties and values.
Formatting normally changes whitespace rather than the underlying data. This means the formatted and minified versions can represent the same JSON document.
How to Format JSON Step by Step
1. Get the JSON you want to format. This could be an API response, configuration object, exported data or another JSON document.
2. Check that the content is intended to be JSON. Make sure the source is not JavaScript, a JSON-like object or another format that only resembles JSON.
3. Open a JSON-compatible formatter. Use a formatter that supports JSON input and produces readable formatted output.
4. Paste or load the JSON. Insert the complete JSON document into the formatter.
5. Format the JSON. Run the formatter to apply indentation, line breaks and consistent spacing.
6. Review the structure. Check nested objects, arrays, property names and values to make sure the output represents the expected data.
7. Copy the formatted result. Use the readable JSON for debugging, inspection, documentation or development work.
Example of a JSON Object
A simple JSON object can contain properties such as a name, status and identifier. Formatting places each property on its own line and makes the opening and closing braces easier to identify.
For example, a compact object such as {'name':'MartTools','status':'active','version':1}, can be expanded into a more readable multi-line structure.
Formatting Nested JSON Objects
JSON objects can contain other objects, creating multiple levels of nesting. Without indentation, deeply nested data can be difficult to follow.
A formatter uses indentation to show the relationship between parent and child objects, making nested structures easier to inspect.
Formatting JSON Arrays
JSON arrays contain ordered collections of values and can contain strings, numbers, objects, arrays or other valid JSON values.
When an array contains multiple complex objects, formatting each object and its properties on separate lines can make the structure substantially easier to understand.
Formatting JSON API Responses
API responses are often returned as compact JSON because machines do not require the visual spacing that humans need. When debugging an API response, formatted JSON can make the returned data much easier to inspect.
Formatting can help you locate properties, inspect nested response objects and understand how collections are structured.
Formatting JSON Configuration Files
JSON is frequently used for configuration files. Consistent indentation makes these files easier to maintain and reduces the effort required to locate individual settings.
When editing configuration files, preserve valid JSON syntax after formatting and avoid adding comments or syntax that standard JSON does not support.
JSON Syntax Rules to Remember
JSON objects use double-quoted property names, and property values must use valid JSON types such as strings, numbers, booleans, null, objects or arrays.
JSON does not use trailing commas after the final property or array item. Strings also require double quotes rather than single quotes.
Formatting Does Not Fix Every JSON Error
A formatter can sometimes identify invalid JSON and report a syntax problem, but formatting and validation are different tasks.
If the JSON contains an unexpected character, missing comma, missing quotation mark or another syntax error, correct the underlying problem before expecting valid formatted output.
Common JSON Formatting Errors
Common problems include missing commas between properties, missing quotation marks, single-quoted strings, trailing commas and unmatched braces or brackets.
Another frequent issue is confusing JavaScript object syntax with JSON syntax. JavaScript objects can support syntax that standard JSON does not.
JSON Formatting vs JSON Validation
Formatting focuses on presentation, while validation determines whether the content follows JSON syntax rules.
A useful workflow is to validate the JSON and then format it for readability. If a formatter reports an error, inspect the indicated location and correct the invalid syntax.
Why Indentation Matters in JSON
Indentation provides a visual representation of nesting. It helps you see which properties belong to an object and which objects belong to an array.
Consistent indentation becomes particularly valuable when JSON contains several levels of nested data.
Choosing an Indentation Style
JSON formatters may allow different indentation settings, such as spaces or tabs and different numbers of spaces per nesting level.
The exact indentation width does not change the JSON data. Choose a style that matches the conventions of the project or document where the formatted JSON will be used.
How to Pretty Print JSON
Pretty printing is another common term for formatting JSON into a human-readable layout. It typically adds indentation, line breaks and spacing while preserving the underlying data.
A JSON pretty printer can be useful when a response or file is currently minified and you need to inspect its structure.
How to Format JSON Online
An online JSON formatter can be useful when you need to quickly inspect JSON without installing a development tool. Paste the JSON into the formatter, run the formatting process and review the result.
Before using an online service, consider whether the JSON contains private information, credentials, tokens or other sensitive data.
Privacy When Formatting JSON Online
JSON can contain more than configuration or public data. API responses may include personal information, authentication tokens, internal identifiers or other confidential values.
Avoid submitting sensitive JSON to an online service unless you understand how the service handles the submitted content. For confidential data, a local formatting tool may be more appropriate.
Formatting JSON for Debugging
Formatted JSON can make debugging easier by exposing the structure of returned data. You can quickly locate nested properties, inspect arrays and compare related objects.
When debugging an application, remember that formatting only changes presentation. It does not change whether the API returned the correct values.
Formatting JSON for Documentation
Readable JSON examples are easier for developers to understand when included in technical documentation, API guides and tutorials.
Use representative examples rather than exposing real credentials, personal information or other production data in documentation.
Common Mistakes When Formatting JSON
One mistake is assuming that formatting automatically makes invalid JSON valid. Another is changing property names or values while trying to clean up the appearance of the document.
It is also important to distinguish JSON from JavaScript objects because similar-looking syntax does not always follow the same rules.
Try the MartTools Code Formatter
Need to make JSON easier to read? Use the MartTools Code Formatter to format supported JSON into a cleaner, more structured layout.
Paste your JSON into the tool, format it and review the resulting structure before copying it into your project, documentation or debugging workflow.
Related tool
Put this guide into practice
Related guides
Continue reading
Frequently asked questions
How do I format JSON?
Paste valid JSON into a JSON-compatible formatter and run the formatting process to add consistent indentation, spacing and line breaks.
What is a JSON formatter?
A JSON formatter is a tool that reorganizes valid JSON into a more readable structure using indentation, line breaks and consistent spacing.
What is pretty printed JSON?
Pretty printed JSON is JSON arranged with indentation, line breaks and spacing so that its structure is easier for people to read.
What is minified JSON?
Minified JSON is JSON with unnecessary whitespace and line breaks removed to produce a more compact representation.
Does formatting JSON change the data?
Formatting normally changes whitespace and presentation without changing the underlying JSON properties and values.
Can a JSON formatter fix invalid JSON?
A formatter may identify syntax errors, but formatting and validation are separate tasks. Invalid JSON may need to be corrected before it can be formatted successfully.
Why does JSON use double quotes?
Standard JSON syntax requires property names and string values to use double quotes.
Can JSON have trailing commas?
Standard JSON does not allow a trailing comma after the final property in an object or the final item in an array.
Can I format nested JSON?
Yes. Formatting is particularly useful for nested JSON because indentation makes relationships between objects and arrays easier to see.
Can I format a JSON API response?
Yes. Formatting an API response can make nested objects, arrays and properties much easier to inspect during development and debugging.
Can I format JSON online?
Yes. Online JSON formatters can format supported JSON directly in a browser without requiring a local development setup.
Is it safe to format private JSON online?
You should review the privacy characteristics of the service before submitting private JSON. Avoid exposing passwords, authentication tokens, personal information or confidential data.