A JSON to PHP converter is useful when an API sample, fixture, or configuration value needs to become PHP source. This tool parses the JSON, maps its values to PHP literals, and places the result in a complete <?php return ...; file.
The conversion is structural, not a schema migration. Objects become keyed PHP arrays, JSON arrays become unkeyed PHP arrays, and scalar values keep their closest PHP equivalents. Review the result before using it in an application, especially when exact number precision or key types matter.
How to Use the JSON to PHP Converter
Convert JSON to PHP array syntax, or switch direction when you need the paired parser.
- Select JSON to PHP in the Direction control.
- Paste valid JSON into the JSON field.
- Choose Short [ ] or array( ) from Array syntax.
- Choose 4 spaces, 2 spaces, or Tab from Indent.
- Review the generated code in PHP array; invalid JSON is reported with parser details and, when available, a line and column.
- Select Copy to copy the output, or Swap to place it in the input and switch to PHP to JSON.
How JSON Values Map to PHP Literals
JSON and PHP share several primitive concepts. JSON strings become single-quoted PHP strings, booleans become true or false, and null remains null. Numbers are emitted as PHP numeric literals after the browser has parsed them. The converter escapes backslashes and apostrophes inside strings for the generated single-quoted syntax.
JSON arrays are emitted as PHP lists without explicit keys. JSON objects become associative arrays with key => value entries. For example:
{"name":"Mina","roles":["admin","editor"],"active":true}
becomes a return value shaped like this when Short [ ] is selected:
<?php
return [
'name' => 'Mina',
'roles' => [
'admin',
'editor',
],
'active' => true,
];
An empty JSON array becomes [], while an empty JSON object also becomes [] in short syntax because both map to an empty PHP array literal. That means the original distinction between {} and [] cannot be recovered from the generated empty value alone.
Short Brackets, Long Array Syntax, and PHP Compatibility
The Short [ ] option writes modern bracket syntax. The array( ) option writes the longer array(...) form. Both express PHP arrays; the difference is source syntax rather than a change to the represented values. Short array syntax has been available since PHP 5.4, while the long form can be useful when maintaining substantially older code or following an existing project convention.
Indent affects nested output and can use two spaces, four spaces, or tabs. It does not change the PHP value. The converter also includes trailing commas on non-empty array entries, which makes later edits and line-based diffs easier to read.
The output is a PHP file that returns the converted value. It does not create a variable, class, object, JSON decoder call, or framework-specific configuration wrapper. A top-level JSON string, number, boolean, or null therefore produces a return file containing that scalar even though the output field is labeled PHP array.
If the destination expects JSON rather than PHP source, use the JSON formatter. To convert in the other direction, use PHP to JSON.
Common Conversion Mistakes and Precision Limits
The input must be standard JSON. Property names and string values require double quotes; comments, trailing commas, single-quoted strings, undefined, NaN, and Infinity are rejected by JSON.parse. Correct the first parser error before interpreting later ones, because a missing quote or comma can shift where the browser reports the failure.
Large JSON integers need special care. The browser parses numbers as JavaScript Number values before generating PHP, so integers outside JavaScript's safe integer range may already have lost precision. Keep identifiers, account numbers, and other exact large values as quoted JSON strings when their digits must remain unchanged.
Object property names that look like safe decimal integers are emitted as unquoted PHP integer keys; other keys remain quoted strings. PHP itself also converts certain valid decimal string keys to integer keys. If the distinction between a numeric-looking string key and an integer key is important, test the generated array in the target PHP version rather than assuming a textual quote will preserve every key semantic.
Conversion also does not validate an application contract. Valid JSON can still contain an unexpected type, missing field, unsafe path, or untrusted value. Treat the generated source as a representation of the supplied data, not evidence that the data is suitable for production.
Privacy and Safe Use of Generated PHP
Parsing and generation run locally in the browser; the tool code does not upload the pasted JSON to a conversion service. That reduces unnecessary exposure, but it does not make production secrets appropriate for routine copy-and-paste workflows. Redact access tokens, customer data, credentials, and private configuration before sharing output or placing it in a ticket.
Generated PHP is source code containing data literals. The converter does not execute the output, and it does not add calls such as eval(). Keep that separation: save reviewed output as a PHP file or incorporate the literal through normal source control rather than evaluating arbitrary text received from another person.
Escaping makes the generated strings syntactically usable as single-quoted PHP literals, but it is not contextual output encoding for HTML, SQL, shell commands, or URLs. Apply the correct validation and escaping at the point where the values are later used.
Frequently Asked Questions
Does the converter generate a complete PHP file?
Yes. It wraps the converted value in <?php and a return statement, ending with a semicolon. It does not add a closing ?> tag, variable assignment, namespace, or class.
What is the difference between Short [ ] and array( )?
They are two PHP syntaxes for arrays. Short [ ] produces bracket literals, while array( ) produces the older long form. The selected syntax changes presentation, not the data mapping or indentation choices.
Why did a large integer change after conversion?
JSON is parsed through JavaScript's numeric type, which cannot represent every integer exactly beyond Number.MAX_SAFE_INTEGER. Once rounding occurs during parsing, the generator cannot reconstruct the original digits. Quote exact large identifiers as JSON strings before conversion.
Can this tool convert JSON objects as well as arrays?
Yes. A JSON object becomes an associative PHP array, and a JSON array becomes a PHP list. Empty objects and empty arrays both produce an empty PHP array literal, so that empty-container distinction is lost.
Is pasted JSON sent to a server?
No conversion request is made; parsing and PHP generation happen in the browser. Sensitive values can still leak through screenshots, clipboard history, browser extensions, or where you paste the result, so redaction remains a sensible precaution.
References: PHP manual: Arrays · RFC 8259: The JSON Data Interchange Format.