About the JSON to PHP
PHP programmers use the standard json_decode() function to work with JSON data. By default, it returns an untyped associative array or stdClass object. These types lack many of the modern development features like IDE autocomplete suggestions and type safety. PHP 7.4 and later support typed properties, which suit Data Transfer Objects (DTOs). However, it can be tiresome to write these classes for larger applications with deeply nested JSON structures. Instead, this JSON to PHP converter can be used to automatically generate strictly typed classes from your JSON data. This tool was built with a strong privacy-first focus and all your data stays secured inside your browser.
Key Features of JSON to PHP
- Typed Properties GenerationOutput modern PHP code. Select "Typed classes" option from the "Output style" dropdown to infer PHP types from JSON schema. For example, it will output public string $$name;, public int $id;.
- Deep Nesting ResolutionResolve complex nesting structures with ease. The converter will automatically detect nested objects (such as the address in the example) and arrays.
- Custom Root NamingYou can customize the root class name using the "Root class name" field. It will be used for the top-level class and follows your naming conventions. Default root class name is Root.
- 100% Local ProcessingYour privacy is important. Your data is processed in your browser and not sent to an external server.
- Instant Visual FeedbackClick the "Sample" button to see how the converter processes a sample JSON object with strings, integers, booleans, and arrays.
How to Use JSON to PHP
- Open the JSON to PHP class converter online tool.
- Paste your JSON payload into the "JSON input" textarea.
- Enter your preferred PHP class name in the "Root class name" field.
- Make sure the "Output style" dropdown is set to "Typed classes".
- Click the purple "Generate" button to see your strictly typed PHP classes.
- Your generated classes with strongly enforced types will appear in the lower textarea.
- Click the "Copy" button to copy the code to your system's clipboard or "Download" to save it as a .php file on your computer.
JSON to PHP Examples
A class with typed public properties.
{"id":7,"name":"Amal","active":true,"tags":["admin"]}<?php
class Root
{
public int $id;
public string $name;
public bool $active;
public array $tags;
}