JSON to Code

JSON to PHP

Paste a JSON sample. Get PHP types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.

Open the tool with PHP chosen

An example

This JSON:

{
  "id": 42,
  "name": "Ada Lovelace",
  "active": true,
  "joined": "2020-01-02T03:04:05Z",
  "tags": ["engineer", "writer"],
  "address": { "city": "London", "postcode": null },
  "scores": [9.5, 8, 10]
}

becomes this PHP:

<?php

// This is an autogenerated file:Root

class Root {
    private int $id; // json:id Required
    private string $name; // json:name Required
    private bool $active; // json:active Required
    private DateTime $joined; // json:joined Required
    private array $tags; // json:tags Required
    private Address $address; // json:address Required
    private array $scores; // json:scores Required

    /**
     * @param int $id
     * @param string $name
     * @param bool $active
     * @param DateTime $joined
     * @param array $tags
     * @param Address $address
     * @param array $scores
     */
    public function __construct(int $id, string $name, bool $active, DateTime $joined, array $tags, Address $address, array $scores) {
        $this->id = $id;
        $this->name = $name;
        $this->active = $active;
        $this->joined = $joined;
        $this->tags = $tags;
        $this->address = $address;
        $this->scores = $scores;
    }

    /**
     * @param int $value
     * @throws Exception
     * @return int
     */
    public static function fromID(int $value): int {
        return $value; /*int*/
    }

    /**
     * @throws Exception
     * @return int
     */
    public function toID(): int {
        if (Root::validateID($this->id))  {
            return $this->id; /*int*/
        }
        throw new Exception('never get to this Root::id');

and 577 more lines. Open the tool to see the whole file.

With the reader and the writer

The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.

<?php

// This is an autogenerated file:Root

class Root {
    private int $id; // json:id Required
    private string $name; // json:name Required
    private bool $active; // json:active Required
    private DateTime $joined; // json:joined Required
    private array $tags; // json:tags Required
    private Address $address; // json:address Required
    private array $scores; // json:scores Required

    /**
     * @param int $id
     * @param string $name
     * @param bool $active
     * @param DateTime $joined
     * @param array $tags
     * @param Address $address
     * @param array $scores
     */
    public function __construct(int $id, string $name, bool $active, DateTime $joined, array $tags, Address $address, array $scores) {
        $this->id = $id;
        $this->name = $name;
        $this->active = $active;
        $this->joined = $joined;
        $this->tags = $tags;
        $this->address = $address;
        $this->scores = $scores;
    }

    /**
     * @param int $value
     * @throws Exception
     * @return int
     */
    public static function fromID(int $value): int {
        return $value; /*int*/
    }

    /**
     * @throws Exception
     * @return int
     */

and 582 more lines. Open the tool to see the whole file.

What you need in your project

Nothing. `json_decode` is in the standard library.

The file holds a class for each object with a reader and a writer, and it checks each field as it reads.

The output is long, because PHP has no way to describe a shape in the language itself.

The trap to know about

A large integer in PHP depends on the platform. On a 32-bit build it turns into a float with no warning.

The options you can change

These belong to PHP, so the row of switches changes with the language.

OptionWhat it doesDefault
Create GetterCreate Gettertrue
Getter without validationgetter without validationfalse
Create SetterCreate Setterfalse
PHP Closing TagPHP Closing Tagfalse

Questions

How do I turn JSON into PHP?

Paste your JSON into the box, choose PHP, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.

Does my JSON go to a server?

No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.

What do I need to add to my PHP project?

Nothing. `json_decode` is in the standard library.

How does it know which fields are optional?

From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.

Other languages

JSON to JavaScript JSON to Flow JSON to Ruby All the guides

Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.

Open JSON to Code