Mock JSON · json_encode() · Laravel · Symfony — 3 modes

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.

Your data never leaves your browser
PHP class → mock JSON
json_encode() snippets
Laravel · Symfony · WordPress
Always free
PHP to JSON — 3 modes   100% client-side
PHP class input
  Ready

      

Three ways to convert PHP to JSON

Each mode targets a different workflow — pick the one that fits.

Mode 1 — Mock JSON

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.

Input
public int $id,
public string $name,
public bool $active
Output
{"id":1,"name":"Alice",
 "active":true}
Mode 2 — json_encode() snippets

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.

Flags output
$json = json_encode($data,
  JSON_PRETTY_PRINT |
  JSON_UNESCAPED_UNICODE |
  JSON_UNESCAPED_SLASHES |
  JSON_THROW_ON_ERROR);
Mode 3 — Framework patterns

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.

Laravel
return response()->json([
  'data' => UserResource
    ::collection($users),
  'meta' => ['total' => $n]
]);

json_encode() flags reference

The most useful JSON_* constants and when to use each.

FlagEffectWhen 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

When do you need PHP to JSON?

Laravel REST APIs

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 test fixtures

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 ACF to JSON

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.

Config and export files

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.

All code to json tools

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.

Constructor promotion parser

Parses PHP 8 constructor promotion syntax — public int $name, — and traditional property declarations with typed hints.

All json_encode() flags

Every useful JSON_* constant covered with explanations — combined into the real-world pattern most PHP developers actually need.

3 frameworks + vanilla

Laravel, Symfony, WordPress and vanilla PHP — the four most common PHP environments where JSON serialization matters.

52 tools, always free

No watermarks, no account. Funded by non-intrusive display advertising only.

Frequently asked questions

Common questions about converting PHP to JSON.

How do I convert a PHP array to JSON?

Use json_encode(): $json = json_encode($array). For pretty output: json_encode($array, JSON_PRETTY_PRINT). Always use JSON_THROW_ON_ERROR to catch encoding errors. Combine flags with |: json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR).

How do I convert a PHP object to JSON?

json_encode() works with objects — it serializes public properties: $json = json_encode($object). For full control over the output, implement the JsonSerializable interface and define a jsonSerialize() method that returns the array you want encoded. Select the JsonSerializable scenario in Mode 2 for the complete pattern.

How do I return JSON from a Laravel controller?

Use return response()->json($data) for a JsonResponse. For API endpoints, create an API Resource: php artisan make:resource UserResource, then return new UserResource($user). For collections: return UserResource::collection($users). Laravel also serializes Eloquent models returned directly from controllers.

What json_encode() flags should I always use?

At minimum: JSON_THROW_ON_ERROR — makes encoding failures throw a JsonException instead of silently returning false. For APIs: also add JSON_UNESCAPED_UNICODE (keeps UTF-8 characters readable) and JSON_UNESCAPED_SLASHES (keeps URLs clean). Combine: json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR).

Is the PHP to JSON converter free?

Yes, completely free. No account required. JSONSwitcher is funded by non-intrusive display advertising.
Go up