JSON to PHP Array, Python Dict and Ruby Hash Converter
Paste JSON and get a ready literal for PHP, Python, Ruby or JavaScript, or paste a literal from code and get clean JSON again.
Draait in je browser. Je bestanden verlaten je apparaat nooit. Zie hoe je dit controleert
…
Zo doe je het
- 1Choose the direction
Tap JSON → code to write a literal, or Code → JSON to read one.
- 2Paste or open the input
Paste text, use Open file for a .json, .php, .py, .rb or .js file, or tap Sample.
- 3Pick the language
In JSON → code choose PHP, Python, Ruby or JavaScript; in Code → JSON leave Detect or choose the source language.
- 4Set the style
Choose quotes, indent, short PHP arrays, Ruby symbol keys and trailing commas.
- 5Name the variable
Keep Wrap ticked and type a variable name, or untick it to get only the literal.
- 6Copy or download
Press Copy, or download the result as a file with the right extension; use Swap to check the round trip.
Over deze tool
JSON is the format APIs speak, but the code that tests or seeds those APIs is written in a programming language. A PHP unit test wants an array with the arrow syntax, a Django fixture or a pytest parametrize block wants a Python dict with True, False and None, a Rails seed file wants a Ruby hash, and a Jest test wants a JavaScript object without quotes around simple keys. Rewriting a fifty-line response by hand means changing every colon, every true and every null, and one missed quote breaks the file. This converter does the rewrite in both directions. In the JSON to code direction it walks the parsed data and prints a literal for the language you pick, escaping quotes, backslashes, dollar signs and control characters so the string means exactly the same thing in the target language. In the code to JSON direction a tolerant reader understands PHP array() and short [ ] syntax, Python dicts, lists and tuples, Ruby hashes with rocket or symbol keys, and JavaScript object literals, including comments, single quotes and trailing commas.
Options that matter in real code
Pick single or double quotes to match your coding standard. PHP gets either the short [ ] syntax that every supported PHP version accepts or the older array( ) form for legacy code. Ruby can use symbol keys (name: value) when the key is a valid symbol, falling back to quoted strings with => for keys that contain spaces or dashes. JavaScript keys are left unquoted when they are valid identifiers. Indent can be any width from zero (one line, handy for a test parameter) to eight spaces, and you can add a trailing comma after the last item so version-control diffs stay small when someone appends a field. The wrap option adds a variable assignment such as $data = [...]; or data = {...} with the name you choose.
How types are mapped
JSON null becomes null in PHP and JavaScript, None in Python and nil in Ruby. true and false become True and False in Python. Numbers are written as they are; integer-like object keys stay integers in PHP, because PHP itself converts the string key 5 into the integer 5. When reading code, Python tuples become JSON arrays, Ruby symbols become strings, and PHP items without a key get the next integer index, exactly as PHP numbers them.
What the reader does not do
This is a data converter, not an interpreter. It does not evaluate expressions, constants, function calls, string concatenation or variables inside a literal, so a value like PHP_EOL or 60 * 60 stops with a clear message and its line and column. Python float('inf') and NaN have no JSON form and are written as null. PHP single-quoted strings keep backslash sequences literally, as PHP does, so choose the source language instead of Detect when that matters.
Uitgewerkt voorbeeld
A Laravel developer copies an API response of 38 lines into the box: an order with id 1042, the name O'Brien & Sons, active true, discount null, two tags and a nested address. With PHP, single quotes, short syntax and a 4-space indent, the tool prints $data = [ 'id' => 1042, 'name' => 'O\'Brien & Sons', 'active' => true, 'discount' => null, ... ]; with the apostrophe escaped. Switching the target to Python gives 'active': True and 'discount': None for a pytest fixture. Pressing Swap feeds the Python output back as input, and the JSON produced is identical to the original response, which confirms the round trip.
In het kort
- Categorie: Ontwikkelaar & data
- Draait in je browser, geen registratie, geen upload.
- De gegevens die je invoert blijven op je apparaat.
- Beschikbaar in het Engels.
- Inhoud voor het laatst gecontroleerd:
- Officiële bron: IETF RFC 8259 (JSON) and the PHP, Python and Ruby language references
Veelgestelde vragen
Which PHP syntax does it produce?
By default the short array syntax with square brackets and =>, which PHP has accepted since 5.4. Untick Short array syntax to get array( ) for very old code bases.
Can it read a Python dict copied from a debugger?
Yes. Single or double quotes, True, False, None, tuples, trailing commas and # comments are all understood, and the result is standard JSON with double quotes.
Why did my Ruby keys become symbols?
Because Symbol keys is ticked. Keys that are not valid symbols, such as first-name, are still written as quoted strings with =>. Untick the option to quote every key.
What happens to numbers that are too big?
JSON numbers are read as JavaScript doubles, so integers above 9,007,199,254,740,991 lose precision. Keep very large IDs as strings in the source JSON if exactness matters.
Does the code to JSON direction run my code?
No. A small parser reads only literal values. Nothing is executed, which is why variables, function calls and arithmetic inside a literal are reported as errors.
Is my data sent anywhere?
No. The conversion runs in your browser, and the last input and options are kept in local storage on this device only.
