Ruby to JSON Converter
Paste Ruby classes, hashes, or objects. Get clean JSON back.
Input
Output
Paste your Ruby code on the left and press Convert.
Or drop a file on this box, , or .
What this tool does
Your Ruby code already describes the data: an Order with attr_accessors, a Customer built with keyword arguments, a few hashes of line items. When a front-end developer asks for the matching JSON, opening rails console to call to_json is overkill. Paste the classes and the .new call here and the right panel shows the JSON, nested objects expanded. A single class, a hash literal or a handful of Rails models pasted together all work.
The converter understands how Ruby actually serializes. The json stdlib turns :symbol keys into strings (because JSON only has string keys), nil becomes null, and Time / Date come out as ISO-8601 strings. Hashes with symbol keys and string keys are both normalised, and arrays of mixed types stay intact. Nested instances, an Address inside an Order, are expanded into nested JSON objects, not stringified. You can read the details in the JSON module docs.
Paste a full file from a gem or your Rails app and every top-level class comes out as its own JSON entry, with initialize-set instance variables pulled into the output. attr_accessor, attr_reader, and plain @ivars are all picked up. If the parser sees a Struct or an OpenStruct, it treats the members the same way. Methods are skipped, only data makes it into the JSON.
How to use it
Three steps. A one-line hash and a whole app/models directory pasted together are handled the same way.
Paste your Ruby (or try the sample)
Paste the Ruby on the left without trimming it. Class definitions, hash literals with symbol keys, Struct declarations and .new calls with keyword arguments are all understood. Load Sample shows an Order whose customer is a Customer.new(...) and whose items are plain hashes.
You do not need to trim the code: keep the Ruby syntax, require lines, and comments as they are. The parser ignores what is not data. For edge cases, the JSON stdlib docs are a good read.
Hit Convert
Press Convert. The code travels to the backend, which follows initialize to see which instance variable receives which argument, then returns JSON with the symbol keys turned into strings. While the request is out the editors are dimmed under a "Converting..." label.
Copy the JSON
The right panel fills with indented, spec-compliant JSON. Copy it into a Postman request, a test fixture, a Rails seeds file, or your API docs.
When this actually comes in handy
Test fixtures for RSpec
You have an Order model with 20 attributes and need JSON to stub an external service. Paste the class, get the payload, drop it into your spec.
Seeding a Rails app
Turn a bunch of Product.new(...) lines into a JSON seed file for db/seeds or a staging data loader, no hand-typing.
Docs that match your models
Writing docs for a Rails or Sinatra endpoint? Paste the model and a sample instance, and the example body in your docs comes from the same attributes the code defines.
Debugging serialization issues
When to_json is behaving oddly, paste the Ruby here to see the expected JSON shape and compare it against what your app is actually sending.
Common questions
Can I paste multiple classes at once?
Yes, paste the lot. Every top-level class shows up as its own JSON entry, and a Customer.new(...) passed into an Order is nested inside it. def methods and private sections describe behaviour rather than data, so only the instance variable values make it out.
How are symbols and string keys handled?
JSON only has string keys, so :name becomes "name": same behaviour as the json stdlib. If you have a hash that mixes symbol and string keys, both are normalised to strings in the output.
What about Time, Date, and BigDecimal?
Time and DateTime come out as ISO-8601 strings. Date is an ISO date string. BigDecimal becomes a JSON number. nil is null. Ranges and regexes are stringified: the rest of the standard types map the way Ruby's default to_json does them.
Does it handle Struct and OpenStruct?
Yes. A Struct with members :name, :price is serialized as a JSON object with those keys. Same for OpenStruct: the dynamic attributes are picked up and emitted.
Is my code stored?
Your Ruby goes to the backend to be converted and is neither kept nor logged there. If the snippet came out of config/credentials or holds an API token, blank those strings out before pasting.
What if the code has methods or blocks?
Methods and blocks are skipped: they are not data. Only the class name, instance variables from initialize, and literal values make it into the JSON, so the rest of your model still comes through cleanly.
Other tools you may need
Need Ruby classes generated from JSON instead, or a second look at this output? Start with these: