JSON to PHP Array and Class Converter
Convert JSON to a PHP array literal with correct escaping, or to PHP 8 classes with typed properties and a fromArray() method. Runs in your browser, so your data is never uploaded.
JSON Input
PHP
What Does This Converter Do?
PHP works with JSON as arrays: json_decode($json, true) turns it into nested associative arrays. This tool writes that structure as PHP code, which is handy for config files, test fixtures, seeders and hard-coded data. It can also generate typed PHP 8 classes, so your code gets autocompletion and type checks instead of string keys.
How to Convert JSON to PHP
- Paste your JSON, or drop a .json file on the box.
- Choose PHP array or PHP 8 classes, and a variable or class name.
- Click Convert to PHP, or press Ctrl + Enter.
- Copy the code, or download it as a .php file.
Example: PHP Array
{
"order_id": "A-2045",
"total": 89.97,
"paid": true,
"note": null,
"customer": { "name": "James O'Brien" },
"tags": ["new", "vip"]
}<?php
$order = [
'order_id' => 'A-2045',
'total' => 89.97,
'paid' => true,
'note' => null,
'customer' => [
'name' => 'James O\'Brien',
],
'tags' => [
'new',
'vip',
],
];JSON objects become associative arrays with =>, JSON arrays become lists, and true, false and null stay as they are. The apostrophe in O'Brien is escaped so the string stays valid.
How Strings Are Escaped
| Text | PHP | Why |
|---|---|---|
it's | 'it\'s' | Single quotes need \' |
C:\new\folder | 'C:\\new\\folder' | Every backslash is doubled, so it never starts an escape |
price $5 | 'price $5' | Single quotes do not replace $ variables |
| Text with a line break | "line1\nline2" | Double quotes are used only when needed, with $ written as \$ |
Every escape was checked by decoding the PHP literal again and comparing it with the original text, including backslashes, quotes, $, tabs, emoji and control characters.
PHP 8 Classes
Choose PHP 8 classes to get one final class per object, with constructor property promotion, typed properties (string, int,float, bool, nullable ? types, nested classes) and a fromArray() factory that maps snake_case keys to camelCase properties:
$data = json_decode($json, true, flags: JSON_THROW_ON_ERROR); $order = Order::fromArray($data); echo $order->customer->name; // James O'Brien
The classes use named arguments and promoted properties, so they need PHP 8.0 or newer. Arrays of objects are mapped with array_map, and a docblock such as /** @var Item[] */ tells your IDE what the array holds. Values whose type the sample does not show become mixed.
Tips
- Number-like keys: PHP turns array keys such as
'1'into the integer1. That is normal PHP behaviour and usually harmless. - Back to JSON:
json_encode($order, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)turns the array back into readable JSON. - Broken JSON?
json_decodereturnsnullon invalid JSON unless you passJSON_THROW_ON_ERROR. Check the input in the JSON Validator or fix it with JSON Repair.
Your JSON never leaves your browser. Need another language? Try JSON to TypeScript, JSON to Java or JSON to Python.
Frequently Asked Questions
How do I convert JSON to a PHP array?
Paste your JSON and click Convert to PHP. The tool writes a PHP array literal with => for object keys, lists for JSON arrays, and true, false and null kept as they are, with every string escaped correctly.
What is the difference between json_decode($json) and json_decode($json, true)?
Without the second argument, json_decode returns stdClass objects that you read with ->. With true, it returns associative arrays that you read with ['key']. This tool writes the array form.
Can it generate PHP classes?
Yes. Choose PHP 8 classes to get typed final classes with constructor property promotion and a fromArray() factory that maps snake_case keys to camelCase properties.
Is my JSON uploaded?
No. The PHP code is generated in your browser and your JSON never leaves your device.