JsonStructMapper provides an easy way to convert JSON files and hashes into Ruby Struct objects, with support for nested structures and arrays. It also supports converting Structs back to hashes.
Add this line to your application's Gemfile:
gem 'json_struct_mapper'And then execute:
$ bundle installOr install it yourself as:
$ gem install json_struct_mapper# Load entire JSON file
converter = JsonStructMapper.from_file('data.json')
# Load specific key from JSON file
converter = JsonStructMapper.from_file('data.json', 'users')
# Access data via struct
converter.object.name
converter.object.email# Load entire JSON file
converter = JsonStructMapper.from_json_file_to_template('data.json')
# Load specific key from JSON file
converter = JsonStructMapper.from_json_file_to_template('data.json', 'users')
# Default struct values
converter.object.name # => nil
# Assign data via struct
converter.object.name = 'Jhon'hash = { name: 'John', age: 30, address: { city: 'NYC' } }
converter = JsonStructMapper.from_hash(hash)
converter.object.name # => 'John'
converter.object.address.city # => 'NYC'empty hash will return nil value
hash = { name: 'John', age: 30, address: {} }
converter = JsonStructMapper.from_hash(hash)
converter.object.address # => niljson_string = '{"name": "John", "age": 30}'
converter = JsonStructMapper.from_json(json_string)
converter.object.name # => 'John'# Convert to hash (removes nil values by default)
hash = converter.to_hash
# Keep nil values
hash = converter.to_hash(compact: false)
# Convert to JSON string
json = converter.to_json
# Pretty print JSON
json = converter.to_json(pretty: true)json = {
user: {
name: 'John',
contacts: [
{ type: 'email', value: 'john@example.com' },
{ type: 'phone', value: '123-456-7890' }
]
}
}
converter = JsonStructMapper.from_hash(json)
converter.object.user.contacts[0].value # => 'john@example.com'The gem provides specific error classes:
- JsonStructMapper::FileNotFoundError - When file doesn't exist
- JsonStructMapper::InvalidJSONError - When JSON is malformed
- JsonStructMapper::InvalidKeyError - When specified key doesn't exist
begin
converter = JsonStructMapper.from_file('missing.json')
rescue JsonStructMapper::FileNotFoundError => e
puts "File error: #{e.message}"
endAfter checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run bundle exec rake install.
Bug reports and pull requests are welcome on GitHub at https://github.com/UppalaShivani/json_struct_mapper.
The gem is available as open source under the terms of the MIT License.