Need to make JSON data readable? This guide covers everything about JSON prettification via API, including formatting options, indentation styles, and implementation examples in multiple programming languages.
What is JSON Prettification?
JSON prettification (also called beautification or formatting) is the process of adding whitespace, line breaks, and indentation to JSON data to make it human-readable. This is the opposite of minification.
Before: {"name":"John","age":30}
After:
{
"name": "John",
"age": 30
}
Formatting Options
Different formatting styles to choose from:
Indentation
Choose between spaces (2 or 4) or tabs for indentation.
Line Breaks
Control where line breaks appear - after each key-value pair or keeping arrays compact.
Sorting Keys
Optionally sort object keys alphabetically for consistent output.
Using the JSON Prettify API
TinyFn provides a simple endpoint to prettify JSON:
POST https://api.tinyfn.io/v1/format/json-prettify
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"json": "{\"name\":\"John\",\"age\":30}",
"indent": 2
}
{
"prettified": "{\n \"name\": \"John\",\n \"age\": 30\n}",
"original_size": 24,
"prettified_size": 35
}
Parameters
| Parameter | Type | Description |
|---|---|---|
json |
string | The JSON string to prettify (required) |
indent |
integer | Number of spaces for indentation (default: 2) |
sort_keys |
boolean | Sort object keys alphabetically (default: false) |
Code Examples
JavaScript / Node.js
const minifiedJson = '{"name":"John","age":30}';
const response = await fetch(
'https://api.tinyfn.io/v1/format/json-prettify',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({ json: minifiedJson, indent: 2 })
}
);
const { prettified } = await response.json();
console.log(prettified);
// {
// "name": "John",
// "age": 30
// }
Python
import requests
minified_json = '{"name":"John","age":30}'
response = requests.post(
'https://api.tinyfn.io/v1/format/json-prettify',
headers={'X-API-Key': 'your-api-key'},
json={'json': minified_json, 'indent': 2}
)
prettified = response.json()['prettified']
print(prettified)
cURL
curl -X POST "https://api.tinyfn.io/v1/format/json-prettify" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"json": "{\"name\":\"John\",\"age\":30}", "indent": 2}'
Common Use Cases
- Debugging: Make API responses readable for debugging
- Documentation: Format JSON examples for documentation
- Logging: Pretty print JSON in log files for easier analysis
- Code Editors: Build JSON formatting features in editors
- Data Display: Show formatted JSON in web interfaces
Best Practices
- Validate first: Ensure JSON is valid before formatting
- Consistent style: Use the same indentation across your project
- Development only: Use prettified JSON for development, minified for production
- Consider sorting: Sorted keys make diffs more readable
Try the JSON Prettify API
Get your free API key and start formatting JSON in seconds.
Get Free API Key