Convert JSON to Ruby Hash Online
Convert JSON to Ruby Hash, Struct, or OpenStruct. Free, in your browser, no file uploads.
{
name: "John",
age: 30,
active: true,
tags: [
"ruby",
"rails"
],
address: {
city: "Tokyo",
zip: "100-0001"
}
}What you can do
JSON to Ruby: idiomatic Hash, Struct, and OpenStruct
Multiple formats
Hash with symbols, Hash with strings, Struct.new, or OpenStruct depending on your usage pattern in Ruby or Rails.
100% private
Conversion happens in your browser with JavaScript. Your code never leaves your device.
Idiomatic Ruby
Generated code following Ruby 3.x conventions and the RuboCop style guide.
Instant
Real-time conversion as you type. No buttons, no waiting.
How it works
Three steps, no hassle
Paste your JSON
Enter any valid JSON object: simple, nested, with arrays or complex fields. Validation happens in real-time in your browser.
Choose the output type
Select Hash with symbol keys, Hash with string keys, Struct.new, or OpenStruct depending on your usage pattern in Ruby or Rails code.
Copy the Ruby code
Get idiomatic Ruby code ready to paste into your project, with correct syntax for the Ruby version you use (2.7+ or 3.x).
FAQ
Got questions?
It depends on the context of use in your Ruby code. Symbol keys (:name, :age) are the dominant pattern in idiomatic Ruby for several reasons: symbols are immutable and unique in memory (Ruby maintains a global symbol table, so :name is always the same object throughout the session), making them more efficient for Hash keys used repeatedly. The modern Ruby syntax (since 1.9) with { name: value } (colon syntax) is cleaner and more readable than the hashrocket { :name => value }. Frameworks like Rails use symbols throughout their internal APIs (params[:user_id], session[:user], flash[:notice]). On the other hand, string keys ('name', 'age') are useful when data comes from external JSON and is manipulated without conversion, when keys contain characters invalid for symbols (hyphens, spaces, dots), or when using ActiveSupport's HashWithIndifferentAccess which accepts both key types interchangeably.
Hash is the most flexible data structure and the default choice for JSON data in Ruby. A Hash has no fixed schema: you can add and remove keys dynamically. It is ideal for configuration data, API responses with variable structure, and any data that varies in structure. Struct.new(:field1, :field2) creates a class with fixed attributes and access through methods (object.field1 instead of object[:field1]). Structs are faster than OpenStruct, consume less memory, are comparable by value (two Structs with the same values are equal), and are a good choice when you know the exact data structure at design time. OpenStruct (from the standard library ostruct) accepts dynamic attributes like a Hash but with method-style access. It is convenient for rapid prototyping but has significant performance penalties: in benchmarks, OpenStruct is 10–50x slower than Hash for attribute access in Ruby 2.x, although Ruby 3.x significantly improved its implementation.
Ruby includes the json library in its standard library since Ruby 1.9 (2007). No additional gem installation is required. Basic usage: require 'json'; data = JSON.parse(json_string). By default, JSON.parse returns a Hash with string keys (the same as in the original JSON). To get symbol keys, use JSON.parse(json_string, symbolize_names: true). The symbolize_names option recursively converts all string keys to symbols, including keys in nested objects. To generate JSON from Ruby: JSON.generate(object) or object.to_json (method added by the json library to Hash, Array, and primitive types). The json_pure gem is the pure Ruby implementation; the json gem (included by default in MRI Ruby/CRuby) uses a C extension for better performance. In JRuby and TruffleRuby, the JSON implementation uses the underlying Java runtime.
Yes, and they are important for production code. OpenStruct has three main performance penalties: (1) Creation: creating an OpenStruct instance is more costly than creating a Hash because it defines accessor methods dynamically for each attribute using define_method. (2) Access: accessing an OpenStruct attribute (object.name) is slower than accessing a Hash (hash[:name]) because it goes through method_missing or the dynamically defined methods, adding method dispatch overhead. (3) Memory: each OpenStruct instance has additional overhead over Hash. In Ruby 2.7 and earlier, benchmarks showed OpenStruct 10–50x slower than Hash. Ruby 3.0 introduced significant improvements to OpenStruct (implementation rewritten in C instead of pure Ruby) reducing the gap, but OpenStruct is still slower than Hash and Struct for intensive access. The official Ruby 3.x recommendation is: prefer Struct over OpenStruct when the schema is known. If you need method-style access with a fixed schema and good performance, use Struct; if you need flexibility for prototyping, use OpenStruct with full awareness of the performance cost.
ActiveSupport::HashWithIndifferentAccess (HWIA) is a Hash subclass included in Rails (and available as the activesupport gem without full Rails) that allows accessing keys interchangeably by string or symbol. It is the type Rails uses for params: params[:user_id] and params['user_id'] return the same value. To load external JSON in Rails with indifferent access: json_data = JSON.parse(response_body).with_indifferent_access. The with_indifferent_access method is available on Hash through ActiveSupport. The conversion is recursive for nested hashes when using HashWithIndifferentAccess.new(hash) but not when using .with_indifferent_access directly on nested hashes (you need to use deep_symbolize_keys or similar for deep conversion). In Rails 7.x, ActionController::Parameters (the params object) implements semantics similar to HWIA but with additional security constraints (requires and permits for Strong Parameters).
Ruby 2.3 introduced the pragma # frozen_string_literal: true that freezes all string literals in the file, making them immutable and shared in memory (similar to how symbols are unique). Ruby 3.x continues promoting this practice and in the Ruby 4.0 roadmap, enabling it by default is discussed. The impact on code generated with string keys for Hash is: with frozen_string_literal active, string keys are automatically frozen, which is memory-efficient (the same string 'name' in multiple hashes can point to the same object if it is a string literal). However, if code attempts to mutate a string key (concatenate, gsub!, etc.) in a file with frozen_string_literal, a FrozenError will be raised at runtime. For code processing JSON with string keys in files with frozen_string_literal, this is rarely a problem because Hash keys are typically not mutated. The recommendation for modern projects on Ruby 3.x is to include # frozen_string_literal: true in all files and use symbol keys in Hashes in your own code for maximum efficiency.
Convert JSON to Ruby Hash, Struct, and OpenStruct: technical guide
Converting JSON to Ruby data structures is a fundamental operation in backend development with Ruby and Ruby on Rails. JSON (JavaScript Object Notation, RFC 7159, updated in RFC 8259 in 2017) uses a data model that maps directly to Ruby's primitive types: JSON objects convert to Hash, JSON arrays to Array, strings to String, numbers to Integer or Float, booleans to true/false, and null to nil. However, the decision of which type of Hash to use (with symbol or string keys) and whether to use Hash, Struct, or OpenStruct has important implications for performance, readability, and maintainability of the resulting Ruby code. Hash syntax in Ruby has evolved significantly across language versions. Ruby 1.8 and earlier only supported the hashrocket syntax: { :key => value }. Ruby 1.9 (2007) introduced the colon syntax for symbol keys: { key: value }, equivalent to { :key => value } but more concise. Ruby 3.1 (2021) extended the colon syntax to allow string keys and variables: { 'key': value } and the value shorthand where the variable name matches the key ({ x:, y: } instead of { x: x, y: y }). RuboCop, the de facto Ruby static code analyzer, recommends in its style guide using the colon syntax ({ key: value }) when all keys are symbols, and hashrocket ({ 'key' => value }) when there are string keys or mixed key types.
Struct in Ruby is a class-generating class: Struct.new(:name, :age) creates a new anonymous class (or named if assigned to a constant) with defined attributes, accessor methods (getters and setters), value comparison (==), and conversion methods like to_a and to_h. Since Ruby 2.5, Struct.new accepts a block where additional methods can be defined. Since Ruby 3.2, Struct.new with keyword_init: true allows initialization with kwargs: Person.new(name: 'Ana', age: 30) instead of Person.new('Ana', 30). Structs in Ruby are widely used for Value Objects (objects representing a conceptual value without their own identity, like a geometric point, a date range, or a postal address) and for Data Transfer Objects (DTOs) in applications following clean architecture patterns. OpenStruct (standard library ostruct, require 'ostruct') is a class that allows creating objects with dynamic attributes defined in the constructor: person = OpenStruct.new(name: 'Ana', age: 30); person.name #=> 'Ana'. Additionally, attributes can be added after creation: person.email = 'ana@example.com'. OpenStruct responds to respond_to? correctly for defined attributes. It is especially popular for writing test doubles, stubs, and fixtures in RSpec and Minitest tests: OpenStruct.new(id: 1, name: 'Test') is a quick way to create fake objects for tests without implementing full classes.
In the context of Ruby on Rails, JSON integration is ubiquitous. Responses from external APIs (REST or GraphQL) are parsed with JSON.parse, params from POST requests with Content-Type application/json are automatically parsed by ActionDispatch and available as ActionController::Parameters (which works like HashWithIndifferentAccess with Strong Parameters). For serializing ActiveRecord models to JSON, Rails includes the as_json method that accepts options like only:, except:, include: for associations, and methods: for virtual methods. The jbuilder gem (included by default in Rails) allows building JSON responses with a Ruby DSL. The ActiveModelSerializers gem and the more recent jsonapi-serializer (based on Netflix's fast_jsonapi) offer model serialization to JSON:API, the most widely used REST API standard in the Rails ecosystem. For converting JSON to Ruby objects with type validation and coercion in modern Ruby projects (2022–2025), the most used gems are: dry-struct (from the dry-rb family) which defines types with automatic coercion and schema validation; Sorbet with T::Struct for static typing at runtime; and ActiveModel::Attributes in Rails which allows defining typed attributes in non-ActiveRecord models. The tool presented here generates the base code that can serve as a starting point for any of these more advanced patterns, or be used directly when simplicity is the priority.