
JSON Formatter: Prettify, Validate, and Lint JSON Like a Pro
JSON formatting is one of those things you do ten times a day but rarely think about. Here's everything you need to know. JSON.stringify: The Basics const data = { name : " Alice " , roles : [ " admin " , " editor " ] }; // Minified (default) JSON . stringify ( data ) // '{"name":"Alice","roles":["admin","editor"]}' // Pretty-printed (2-space indent) JSON . stringify ( data , null , 2 ) // { // "name": "Alice", // "roles": [ // "admin", // "editor" // ] // } // Tab indent JSON . stringify ( data , null , ' \t ' ) The Replacer Function (Secret Power) // Filter sensitive fields JSON . stringify ( data , ( key , value ) => { if ( key === ' password ' || key === ' token ' ) return undefined ; return value ; }, 2 ); // Or pick specific keys JSON . stringify ( data , [ ' name ' , ' email ' ], 2 ); Command Line: jq jq is the JSON Swiss Army knife: # Pretty-print cat data.json | jq '.' # Minify cat data.json | jq -c '.' # Sort keys cat data.json | jq -S '.' # Extract fields cat data.json | jq
Continue reading on Dev.to Python
Opens in a new tab



