How to prettyprint a JSON file?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
JSON (JavaScript Object Notation) is a lightweight data interchange format that's easy for humans to read and write, and easy for machines to parse and generate. When working with JSON files, especially large ones, they can become difficult to read when printed in their compact form. Pretty-printing JSON involves formatting the data with indentation and newlines for improved readability. Here’s how you can pretty-print a JSON file using different programming languages and tools.
Techniques to Pretty-Print JSON
Using Command Line Tools
1. Python
Python’s json module provides built-in support for pretty-printing. Below is a Python script to pretty-print a JSON file.
In this script:
json.load(file)is used to parse the JSON file.json.dumps()withindent=4specifies the number of spaces for indentation.
2. jq (Command-line JSON processor)
jq is a powerful command-line tool for processing JSON data.
Running the command above will print the contents of data.json with default formatting. Adjust the command as needed to output with specific indent levels.
Using Web Tools
There are various online tools available that allow users to upload a JSON file and receive a pretty-printed version. These tools typically offer additional features like error checking and conversion to different formats.
Pretty-Printing JSON in Different Programming Languages
1. JavaScript (Node.js)
JavaScript, being the language JSON is derived from, easily handles JSON data using built-in methods.
JSON.parse(data)parses the JSON string into a JavaScript object.JSON.stringify(jsonData, null, 4)converts the object back to a JSON string with a 4-space indentation.
2. Java
For Java, you can make use of libraries like Jackson or Gson to pretty-print JSON.
Using Jackson:
Using Gson:
Summary Table
Below is a summary table of different ways to pretty-print JSON across various tools and languages:
| Method/Tool | Language/Tool | Command/Syntax |
| Python script | Python | json.dumps(data, indent=4) |
| jq tool | Terminal | jq '.' data.json |
| JavaScript (Node.js) | JavaScript | JSON.stringify(obj, null, 4) |
| Jackson Library | Java | writer.writeValueAsString(json) |
| Gson Library | Java | gson.toJson(jsonObject) |
Additional Considerations
- Whitespace Sensitivity: JSON itself is not whitespace-sensitive and all pretty-printing does not affect its parseability.
- Security: When pretty-printing JSON, be cautious with JSON data from untrusted sources as parsing and serializing may expose your application to injection risks if not properly handled.
- Performance: Large JSON files may take longer to pretty-print due to the additional processing required for indentation and sorting keys.
Incorporating pretty-printing into your workflow can vastly improve the process of debugging and analyzing JSON data, making it a valuable skill for developers working with APIs and data interchange formats.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.