What Is JSON Formatting? A Beginner's Guide to Pretty-Printing & Syntax Rules
Understand why APIs output minified JSON, how formatters parse and re-indent payloads with 2 or 4 spaces, and how to safely inspect JSON payloads directly in your browser.
EveryZap — 100% Free & Private Tools
Discover why JavaScript provides two URL encoding methods, which characters each one escapes, and how to safely encode query parameters without corrupting full URLs.
When transmitting data over the web via HTTP GET requests, URLs are restricted to a defined subset of ASCII characters according to IETF RFC 3986. Characters outside this safe set—including spaces, non-Latin characters, and symbols that carry structural syntactic meaning in a URL—must be converted into percent-encoded representations.
JavaScript provides two global built-in functions to handle this: encodeURI() and encodeURIComponent(). While both perform percent-encoding (replacing unsafe characters with a percent symbol followed by their two-digit hexadecimal ASCII representation), they serve fundamentally different architectural roles.
Choosing the wrong function is one of the most common causes of broken API queries, corrupted authentication tokens, and malformed redirect URLs in frontend web development.
The key difference lies in which reserved characters each function leaves unencoded. Standard URI delimiters like ?, =, &, /, and # are essential for defining URL structure. If encoded in a full URL, the browser cannot find the path. However, if unencoded inside a query parameter value, they prematurely split the query string.
| Character | Description | encodeURI() Encodes? | encodeURIComponent() Encodes? |
|---|---|---|---|
| ; | Semicolon delimiter | No | Yes (%3B) |
| / | Path segment separator | No | Yes (%2F) |
| ? | Query string initiator | No | Yes (%3F) |
| : | Protocol / Port separator | No | Yes (%3A) |
| @ | Userinfo separator | No | Yes (%40) |
| & | Query parameter separator | No | Yes (%26) |
| = | Key-value delimiter | No | Yes (%3D) |
| + | Sub-delimiter / Space | No | Yes (%2B) |
| $ | Sub-delimiter | No | Yes (%24) |
| , | Comma separator | No | Yes (%2C) |
| # | Fragment identifier | No | Yes (%23) |
| Space | Whitespace | Yes (%20) | Yes (%20) |
encodeURI() is engineered to operate on an entire, existing URL. It assumes that characters such as ://, /, ?, and & were intentionally placed there by the developer to delineate the protocol, host, pathname, and query segments.
Its primary role is encoding unsafe characters like spaces or international Unicode characters that exist within pathnames, while keeping the structural framework of the URL intact.
// Safely encode international characters and spaces in full URL
const rawUrl = "https://example.com/search/café résumé?lang=fr";
const safeUrl = encodeURI(rawUrl);
console.log(safeUrl);
// Outputs: https://example.com/search/caf%C3%A9%20r%C3%A9sum%C3%A9?lang=fr
// Notice that ://, /, ?, and = remain unencoded!encodeURIComponent() is designed to encode individual parameter keys and values before they are concatenated into a URL. Because it aggressively encodes all reserved characters including &, =, ?, and /, it prevents user inputs from injecting rogue query parameters or splitting values.
If a user searches for the term "books & stationery", leaving the ampersand unencoded results in two broken parameters (?q=books and &stationery=). encodeURIComponent() converts the ampersand to %26, preserving the string as a single unbroken query value.
// Safely encode query parameter components
const searchKeyword = "Python & Machine Learning: 100%";
const encodedKeyword = encodeURIComponent(searchKeyword);
const apiUrl = `https://api.example.com/items?query=${encodedKeyword}`;
console.log(apiUrl);
// Outputs: https://api.example.com/items?query=Python%20%26%20Machine%20Learning%3A%20100%25A frequent pitfall is passing an already encoded string back through encodeURIComponent(), resulting in double encoding. For instance, %20 becomes %2520, which backend APIs often fail to parse.
Another critical trap occurs during decoding. While decodeURI() and decodeURIComponent() reverse their corresponding encoding routines, calling decodeURIComponent() on a string containing an invalid percent sequence (such as a bare % symbol not followed by valid hex digits) throws an unhandled URIError: URI malformed.
In modern JavaScript, the cleanest way to construct URLs is using the URL and URLSearchParams Web APIs, which automatically apply encodeURIComponent() semantics to query values behind the scenes.
// Modern idiomatic parameter construction
const url = new URL("https://api.example.com/v1/search");
url.searchParams.set("q", "React & Vue.js");
url.searchParams.set("sort", "highest-rated");
console.log(url.toString());
// Automatically outputs: https://api.example.com/v1/search?q=React+%26+Vue.js&sort=highest-ratedEncode and decode URLs, query parameters, and URI components online with UTF-8 Unicode support, URI parsing, and client-side processing. Calculate instantly directly in your browser.
Format, beautify, validate, and minify JSON data online with error location detection, custom indentation, and key sorting.
Encode and decode Base64 and Base64URL strings online with UTF-8 Unicode support, emoji handling, auto-validation, and client-side processing.
Generate random UUID v4 strings and validate UUIDs online with bulk generation, compact format, case toggle, and client-side processing.
Formulas, conversion rules, and technical specifications referenced in this guide are derived from published academic and web standards:
The official ECMAScript standard defining the semantics of encodeURI and encodeURIComponent.
The canonical Internet standard for percent-encoding, reserved characters, and URI grammar.
Comprehensive developer documentation and browser compatibility matrix.
This guide was researched and published by the EveryZap Editorial Team. EveryZap builds free, privacy-first web utilities and publishes documented calculation formulas and specifications. Have feedback, correction, or university formula update? Contact our editors.
Common questions regarding encodeURI vs encodeURIComponent: Key Differences, When to Use Each & Practical Examples.
No. encodeURI preserves forward slashes (/), colons (:), question marks (?), and ampersands (&) because they define standard URL path and query structure. If you need to encode these inside a query parameter, use encodeURIComponent.
The best practice is to encode individual query parameter values using encodeURIComponent() before assembling the final URL, or use the standard browser URLSearchParams API which handles parameter encoding automatically.
This error occurs when decodeURIComponent encounters a percent sign (%) that is not followed by two valid hexadecimal characters (0-9, A-F), or when an incomplete UTF-8 byte sequence is encountered. Always wrap decoding in a try-catch block.
Understand why APIs output minified JSON, how formatters parse and re-indent payloads with 2 or 4 spaces, and how to safely inspect JSON payloads directly in your browser.
Formatters make JSON beautiful; validators ensure it complies with syntax and schema rules. Learn how both processes work, common parse error codes, and debugging workflows.
Ever wonder why images are embedded as long strings starting with data:image/png;base64? Learn how Base64 transforms binary bytes into ASCII text, how padding works, and its 33% overhead.