JSON to PHP

Turn JSON into typed PHP classes, or into a plain PHP array you can paste into code. A starting point you can adjust to your project's style.

JSON to PHP

Data & Format Tools

Runs locally in your browser.

Runs locally in your browser — your JSON is converted on your device and never uploaded.

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

  1. Open the JSON to PHP class converter online tool.
  2. Paste your JSON payload into the "JSON input" textarea.
  3. Enter your preferred PHP class name in the "Root class name" field.
  4. Make sure the "Output style" dropdown is set to "Typed classes".
  5. Click the purple "Generate" button to see your strictly typed PHP classes.
  6. Your generated classes with strongly enforced types will appear in the lower textarea.
  7. 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.

Input
{"id":7,"name":"Amal","active":true,"tags":["admin"]}
Result
<?php

class Root
{
    public int $id;
    public string $name;
    public bool $active;
    public array $tags;
}

JSON to PHP — Frequently Asked Questions

Why should I use typed classes instead of json_decode's associative arrays?
Associative arrays are "dumb bags of data". Your IDE cannot know what keys are present in the array, making it easy to make typos and encounter Undefined index: XXX errors. Using typed classes provides immediate autocomplete suggestions (IntelliSense) from the IDE. It also adds compile-time (or runtime for PHP) checks that your application will fail fast and loudly in production if the API starts returning unexpected data.
What PHP version is this code compatible with?
The "Typed classes" option uses PHP 7.4+ features like typed properties (public string $property).
Is my API payload data safe in this JSON to PHP converter?
This JSON to PHP converter is a local application that runs entirely in your browser. Your JSON data is processed locally and is never sent to or stored on any server.