JSON Formatter, Minifier, and Validator in PHP — Code Examples
JSON parsing and serialisation is built into every major language. Here is how to parse a JSON string, format (pretty-print) JSON, and handle common edge cases in each language.
PHP has built-in json_encode() and json_decode() functions. Pass JSON_PRETTY_PRINT for formatted output.
<?php
// Parse JSON string → array
$data = json_decode('{"name":"Alice","age":30}', true); // true = assoc array
echo $data["name"] . "
"; // Alice
// Parse JSON → object (default)
$obj = json_decode('{"name":"Alice","age":30}');
echo $obj->name . "
"; // Alice
// Serialize array → JSON
$json = json_encode($data);
echo $json . "
"; // {"name":"Alice","age":30}
// Pretty-print
$pretty = json_encode($data, JSON_PRETTY_PRINT);
echo $pretty . "
";
// Pretty-print + Unicode (don't escape non-ASCII)
$pretty2 = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
// Error handling
$result = json_decode("invalid json", true);
if (json_last_error() !== JSON_ERROR_NONE) {
echo "Error: " . json_last_error_msg() . "
";
}
// PHP 8.3+: throws JsonException on error
try {
$data = json_decode("invalid", true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
echo $e->getMessage() . "
";
}- Pass true as the second argument to json_decode() to get an associative array instead of stdClass.
- json_decode() returns null on error — always check json_last_error() or use JSON_THROW_ON_ERROR.
- JSON_UNESCAPED_UNICODE prevents ASCII escaping of characters like © or emoji, keeping the output readable.
Need to json formatter without writing code? The JSON Formatter, Minifier, and Validator runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.
Open JSON Formatter →JSON Formatter, Minifier, and Validator in JavaScript / Node.js
JavaScript has JSON.parse() and JSON.stringify() built in. Use the space parameter of JSON.stringify() for pretty-printing.
JSON Formatter, Minifier, and Validator in Python
Python's json module has json.loads() for parsing and json.dumps() for serialization. Use indent for pretty-printing.
JSON Formatter, Minifier, and Validator in Go
Go's encoding/json package uses struct tags for mapping. json.Marshal() serializes and json.Unmarshal() parses.
JSON Formatter, Minifier, and Validator in Java
Java has no built-in JSON library. Jackson (ObjectMapper) is the standard choice; Gson is also popular.
JSON Formatter, Minifier, and Validator in Ruby
Ruby's standard library includes the json gem. Use JSON.parse to decode and JSON.generate (or JSON.pretty_generate) to encode.
JSON Formatter, Minifier, and Validator in Rust
Rust uses serde_json for JSON serialization and deserialization. Define structs with #[derive(Serialize, Deserialize)] for typed parsing.
JSON Formatter, Minifier, and Validator in C# / .NET
C# uses System.Text.Json (built-in since .NET 3.0) for JSON serialization. Use JsonSerializer.Serialize / Deserialize for typed objects.