JSON to PHP array
Paste JSON and get it as a PHP array literal below, as you type, ready to paste into code, a config file or a test. Or switch to Types for PHP 8 classes that read it.
The array, written out
Objects become arrays of 'key' => value pairs and lists plain arrays, with true, false and null. Strings are in single quotes, or in double quotes when they hold line breaks or tabs. It is the array json_decode($json, true) gives, as code.
Or PHP 8 classes
Types writes a final class for each kind of object, with promoted constructor properties, what its arrays hold in a docblock (list<string>, array<string, Item>), and fromArray, which builds the object from the decoded array, nested objects and lists included. Keys some items lack default to null.
Types from sample data
Every item of a list, and every value of JSON Lines, is a sample of the same type: a key that some lack is optional, a value that is sometimes null is nullable, whole numbers mixed with decimals are decimals, and objects with the same keys share one type, named after the key they are under ("line_items" holds LineItem). An object whose keys are ids or dates, such as {"1001": {…}, "1002": {…}}, is a map. YAML, TOML, CSV and the other formats work too: pick their language above the editor.
Copy, download or switch
The PHP follows your typing, and a mistake in the JSON is pointed out first, with its line and column. Copy or Download (a .php file) take it as it is. Above the code, switch between Types and Data, or pick another language; the Convert menu's Code item opens the one you picked last.
Private: nothing is uploaded
Formatting, checking and comparing all happen in your browser. Your documents are saved in the browser's own storage on this computer, not on a server, so they never leave your device.
With your permission, we count anonymous visits (which pages and tools are used), never what you type. See Privacy.
Questions
How does PHP itself turn JSON into an array?
$data = json_decode($json, true) gives the same array when the program runs; true asks for arrays rather than objects. Add JSON_THROW_ON_ERROR as the fourth argument to get an exception for invalid JSON.
Which PHP do the classes need?
PHP 8.0 or later, for promoted constructor properties, named arguments and the mixed type.
Is my data uploaded anywhere?
Your documents aren't: everything runs in your browser, and they are saved on your computer, in the browser's storage for this site. With your permission we count anonymous visits, never their content.
More tools
- Formatter & Converter for JSON, YAML, XML, SQL
- JSON to TypeScript – Interfaces from JSON
- JSON to Go Struct Converter
- JSON to C# Class Converter
- JSON to Java Class & Record Converter
- JSON to Python Dataclass, Pydantic & Dict
- JSON to Kotlin Data Class Converter
- JSON to Dart Model with fromJson & toJson
- JSON to Swift Codable Struct Converter
- Free Online Diagram Maker & Editor
- Crypto Tools: Passwords, Keys, Hashes & JWTs
- Timestamp, Cron, Regex & UUID Utilities