PHPackages                             kshabazz/json-to-src - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. kshabazz/json-to-src

ActiveApplication[Utility &amp; Helpers](/categories/utility)

kshabazz/json-to-src
====================

A program to convert a JSON file to source code.

0.0.1(9y ago)01.2k[6 issues](https://github.com/b01/json-to-src/issues)MITPHPPHP &gt;=5.5

Since Feb 22Pushed 9y ago1 watchersCompare

[ Source](https://github.com/b01/json-to-src)[ Packagist](https://packagist.org/packages/kshabazz/json-to-src)[ RSS](/packages/kshabazz-json-to-src/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (3)Versions (3)Used By (0)

Description
-----------

[](#description)

Convert JSON to source code.

This tool takes a JSON file, along with other required input, and on success will parse the JSON and output (PHP by default) source files. It works recursively on the JSON, and will parse nested keys where the value is an object. In addition, an array that contain objects will produce a class, but only one since all elements are assumed to be of the same type.

### Features

[](#features)

- Parses JSON into an hash-array to be used in a template engine ([see schema](#template-data-schema)).
- Outputs file(s) to a directory of your choice (assuming it is writable).
- Can turn off/on generating unit tests.
- Optional set a namespace.
- set a callaback to manipulate the output of the source code generated.

#### Using the Default PHP template

[](#using-the-default-php-template)

- Will convert to PHP, turning fields into properties with getters and setters (fluent style).
- Optional scalar type hints for PHP 7 Function signatures. Example: ```
    /**
     * @var int
     */
    private $client;

    /**
     * Get ClientId.
     *
     * @param int $ClientId
     * @return $this;
     */
    public function setClientId(int $ClientId)
    {
        $this->ClientId = $ClientId;
    }
    ```

### Side-effect

[](#side-effect)

Some things to make note of when using this tool include:

- Bad characters like "$,-" Will automatically be removed from keys in the JSON file.
- A field that is an array and has an object as its first element will result in a class being generated.
- All objects found will produce a class file, the key that contained that object will be used as the name.
- Will generate namespaces for nested objects to prevent class name collision.

Here's a rough idea of how it handles JSON types:

JSONPHPnullnull123int1.00double (a.k.a float)"string"string (no interpolation, ex: 'string')"Company": {"name": "Kohirens"}class Company {}"Clients": \[{"id":1}\]class Clients { private $id; getId,setId }"Company": {}Will produce no class, since the class would have no properties.### Installation

[](#installation)

#### Composer.json

[](#composerjson)

```
composer require --dev kshabazz/json-to-src

- or -

{
  "require": {
    "kshabazz/json-to-src": "^1.0"
  }
}
```

#### Shell or Command Line

[](#shell-or-command-line)

```
composer.phar require --dev kshabazz/json-to-src
```

### Template Data Schema

[](#template-data-schema)

This is the array that is passed to the tempalte engines render method:

```
$renderData = [
    "className" => Company
    "classProperties" => [
        [
            "name" => company
            "type" => string
            "isCustomType" =>
            "paramType" => string
            "value" => Kohirens
            "arrayType" =>
        ],
        [
            "name" => employees
            "type" => array
            "isCustomType" =>
            "paramType" => array
            "value" => ""
            "arrayType" => employees
        ],
        [
            "name" => location
            "type" => Location
            "isCustomType" => 1
            "paramType" => Location
            "value" => stdClass Object
                (
                    "type" => Point
                    "coordinates" => Array
                        (
                            [0" => -83.0466419
                            [1" => 42.3323378
                        )

                    "city" => Detroit
                    "state" => MI
                    "zip_code" => 48226
                )

            "arrayType" =>
        ],
        [
            "name" => categories
            "type" => array
            "isCustomType" =>
            "paramType" => array
            "value" => []
            "arrayType" => categories
        ]
    ],
    "classNamespace" => Tests
];
```

### Set A Callback

[](#set-a-callback)

You can also set a callback that will recieve the render data, to control thins such as the class name, namespace, and etc. Take the following example.

**Callback script: preRenderCallback.php**

```
return function (array $renderData, $isUnitTest) {
    if (!$isUnitTest) {
        $renderData['classNamespace'] .= '\\Bar';
    }

    return $renderData;
};
```

**Pass preRenderCallback.php to the command line script:**

```
jsontosrc -n Tests -c preRenderCallback.php company.json Company tmp
```

### Command Line

[](#command-line)

The "jsontosrc" command line script take several arguments and options.

-n Takes a string to use as a namespace. -u A separate directory to output unit test. -a Set the property access, the default is "private." -c A callback function to modify template data before render. -r Control how deep to go for nested objects, the default is 20. -p An optional string to be used as a namespace prefix. -v Add debug messages to the output. -t Turn on PHP 7 type hints.

###  Health Score

25

—

LowBetter than 36% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3408d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/925d9277d50ed15142f489c340899e4405834eb561fabd90dae5c9305df34259?d=identicon)[b01](/maintainers/b01)

---

Top Contributors

[![b01](https://avatars.githubusercontent.com/u/619281?v=4)](https://github.com/b01 "b01 (15 commits)")

---

Tags

phpjsonutilitycodetoolconvertersource

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/kshabazz-json-to-src/health.svg)

```
[![Health](https://phpackages.com/badges/kshabazz-json-to-src/health.svg)](https://phpackages.com/packages/kshabazz-json-to-src)
```

###  Alternatives

[php-collective/dto

Framework-agnostic Data Transfer Object library with code generation

2812.2k6](/packages/php-collective-dto)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
