Convert PHP to JSON
Mock data, snippets
or framework guide.
Generate mock JSON from a PHP class, get
json_encode()
snippets with all common flags, or get Laravel and
Symfony serialization patterns. Three modes, one tool.
Each mode targets a different workflow — pick the one that fits.
Paste a PHP class with constructor promotion or typed properties and get a realistic JSON object. Useful for Postman collections, PHPUnit fixtures and API documentation — without running a PHP server or instantiating the class.
public int $id, public string $name, public bool $active
{"id":1,"name":"Alice",
"active":true}
Get complete PHP code for every json_encode() use case — basic, pretty-print, all useful flags combined, file output, HTTP response headers and the JsonSerializable interface. Each snippet is copy-paste ready with error handling.
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
Ready-to-use JSON serialization patterns for Laravel (JsonResponse, API Resource, Eloquent toJson), Symfony (JsonResponse, Serializer component), WordPress (wp_send_json, ACF fields to JSON) and vanilla PHP.
return response()->json([
'data' => UserResource
::collection($users),
'meta' => ['total' => $n]
]);
The most useful JSON_* constants and when to use each.
| Flag | Effect | When to use |
|---|---|---|
JSON_PRETTY_PRINT |
Indented, human-readable output | Debugging, log files, config file generation |
JSON_UNESCAPED_UNICODE |
UTF-8 chars kept as-is (not \uXXXX) | Any API returning non-ASCII text — names, cities, emoji |
JSON_UNESCAPED_SLASHES |
Forward slashes not escaped | URLs and file paths in JSON output |
JSON_THROW_ON_ERROR |
Throws JsonException instead of returning false | Always — makes errors impossible to ignore silently |
JSON_NUMERIC_CHECK |
Strings that look like numbers become numbers | Only when you have numeric strings from a database |
JSON_FORCE_OBJECT |
Arrays encoded as objects {} | When an empty array must be {} not [] in the response |
Laravel API Resources transform Eloquent models to JSON responses with full control over the output structure. Mode 3 gives you the complete API Resource class, the controller that uses it, and the JsonResponse pattern — ready to drop into a Laravel project.
PHPUnit integration tests that hit API endpoints need to assert against specific JSON structures. Mode 1 generates a realistic JSON object from your PHP class — paste it as the expected response body in assertJson() or create it as a static fixture file for data providers.
WordPress sites using Advanced Custom Fields often need to expose ACF field values as JSON — for headless WordPress setups, REST API extensions or JavaScript-powered themes. Mode 3 gives you the WordPress-specific patterns for wp_send_json() and ACF field serialization.
PHP applications often need to export configuration arrays or database results as JSON files — for data exchange, backup scripts or feeding front-end applications. Mode 2 gives you the file_put_contents() + json_encode() pattern with proper UTF-8 flags and error handling.
PHP parsed in your
browser. No server needed.
The mock JSON mode uses a property-level regex parser — no need to run PHP or install anything. The parser handles constructor promotion (PHP 8), traditional typed properties and untyped properties. The snippets are generated from your selection — no API call, no server round-trip.
The framework patterns cover the actual recommended patterns from Laravel, Symfony and WordPress documentation — not generic examples. The Laravel API Resource, Symfony Serializer and WordPress wp_send_json() patterns are production-ready and follow each framework's conventions.
