Developer and Text Tools

How to Turn JSON into PHP Arrays, Python Dicts and Ruby Hashes

Learn how JSON maps to PHP arrays, Python dicts, Ruby hashes and JavaScript objects, which escapes matter, and how to convert both ways without mistakes.

Saizul Amin
Saizul Amin3 Nov 2026 · 4 min read
How to Turn JSON into PHP Arrays, Python Dicts and Ruby Hashes

Sooner or later every developer copies an API response into a test. The response is JSON, but the test is written in PHP, Python, Ruby or JavaScript, and each language has its own way of writing the same data. Doing the translation by hand is tedious and error-prone. This guide explains how the formats line up, where the traps are, and how to convert quickly in either direction.

The same data, four spellings

JSON has six kinds of values: objects, arrays, strings, numbers, true or false, and null. Every popular language has a literal syntax for all of them, but the details differ.

  • PHP uses one structure, the array, for both lists and maps. Keys are written with a fat arrow: 'name' => 'Ann'. Modern PHP accepts square brackets; very old code uses array( ). Null is null.
  • Python uses dicts with colons and lists with square brackets. The constants are capitalised: True, False and None.
  • Ruby uses hashes with => or, for simple names, the symbol style name: 'Ann'. Null is nil.
  • JavaScript object literals look most like JSON, but keys that are valid identifiers do not need quotes.

Small differences like None against null are exactly what breaks a test when you edit by hand.

Escaping is where bugs hide

Strings need the most care. An apostrophe inside a single-quoted string must be escaped, as in 'O\'Brien'. In PHP and Ruby a double-quoted string treats $ and #{ as the start of interpolation, so a price like $5 silently changes unless the dollar sign is escaped. PHP single-quoted strings, on the other hand, keep \n as two literal characters, while Python turns it into a line break. A good converter knows these rules for each language and escapes only what is necessary.

Keys that change type

PHP has a surprise for anyone coming from JSON. An array key that looks like an integer, such as "5", is stored as the integer 5. Items written without a key get the next free integer. When you read a PHP array back into JSON you therefore get an object with keys like "0", "5" and "6", which is correct even if it looks odd at first. Ruby symbols simply become strings, and Python tuples become JSON arrays.

Converting with the tool

The JSON to PHP Array, Python Dict and Ruby Hash Converter works in both directions in your browser.

  1. Choose JSON → code and paste your JSON, or open a file.
  2. Pick the target language, the quote style and the indent. An indent of zero gives a single line, which suits a data provider or a parameterised test.
  3. For PHP choose short or long array syntax. For Ruby decide whether simple keys become symbols.
  4. Leave Wrap in a variable assignment on and type a name if you want $order = [...]; ready to paste.
  5. Copy the output or download it with the right file extension.

To go the other way, choose Code → JSON and paste a literal from your code or a debugger. The language is detected, but selecting it yourself is safer for PHP and Ruby because of the single-quote rule above. Comments, trailing commas and either quote style are accepted.

Check the round trip

The Swap button takes the output and feeds it back as input in the opposite direction. If the JSON that comes back matches what you started with, nothing was lost. This is a quick habit worth keeping when the data contains apostrophes, backslashes, unicode or deeply nested lists.

Things the converter will not do

The reader understands literal values only. It does not execute code, so constants such as PHP_EOL, arithmetic like 60 * 60, function calls and variables inside a literal are reported with their line and column rather than guessed. Infinity and NaN, which Python and PHP can express, have no JSON form and become null. Very large integers above 2 to the power 53 lose precision in JSON numbers, so keep long IDs as strings.

A practical example

Suppose an order endpoint returns an id, a customer name with an apostrophe, an active flag, a null discount, two tags and an address. For a Laravel feature test you choose PHP with single quotes and get 'active' => true and 'discount' => null with the apostrophe escaped. For a pytest fixture the same data becomes 'active': True and 'discount': None. One paste, two languages, no manual edits.

Summary

Converting between JSON and code literals is mechanical, which is why it is easy to get wrong when tired. Let a tool handle the quotes, escapes and constant names, check the round trip with Swap, and spend your attention on what the test should actually prove.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages