rails/jbuilder sits at 4.4k stars on GitHub, written primarily in Ruby. Jbuilder: generate JSON objects with a Builder-style DSL
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
Jbuilder gives you a simple DSL for declaring JSON structures that beats
manipulating giant hash structures. This is particularly helpful when the
generation process is fraught with conditionals and loops. Here's a simple
example:
# app/views/messages/show.json.jbuilder
json.content format_content(@message.content)
json.(@message, :created_at, :updated_at)
json.author do
json.name @message.creator.name.familiar
json.email_address @message.creator.email_address_with_name
json.url url_for(@message.creator, format: :json)
end
if current_user.admin?
json.visitors calculate_visitors(@message)
end
json.comments @message.comments, :content, :created_at
json.attachments @message.attachments do |attachment|
json.filename attachment.filename
json.url url_for(attachment)
end
You don't always have or need a collection when building an array.
json.people do
json.child! do
json.id 1
json.name 'David'
end
json.child! do
json.id 2
json.name 'Jamie'
end
end
# => { "people": [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ] }
Nested Jbuilder Objects
Jbuilder objects can be directly nested inside each other. Useful for composing objects.
class Person
# ... Class Definition ... #
def to_builder
Jbuilder.new do |person|
person.(self, :name, :age)
end
end
end
class Company
# ... Class Definition ... #
def to_builder
Jbuilder.new do |company|
company.name name
company.president president.to_builder
end
end
end
company = Company.new('Doodle Corp', Person.new('John Stobs', 58))
company.to_builder.target!
# => {"name":"Doodle Corp","president":{"name":"John Stobs","age":58}}
Rails Integration
You can either use Jbuilder stand-alone or directly as an ActionView template
language. When required in Rails, you can create views à la show.json.jbuilder
(the json is already yielded):
# Any helpers available to views are available to the builder
json.content format_content(@message.content)
json.(@message, :created_at, :updated_at)
json.author do
json.name @message.creator.name.familiar
json.email_address @message.creator.email_address_with_name
json.url url_for(@message.creator, format: :json)
end
if current_user.admin?
json.visitors calculate_visitors(@message)
end
Partials
You can use partials as well. The following will render the file
views/comments/_comments.json.jbuilder, and set a local variable
comments with all this message's comments, which you can use inside
the partial.
You can also render to a collection of partials inline under a key.
json.comments @post.comments, partial: 'comments/comment', as: :comment
# => { "comments": [{ "content": "Hello everyone!" }, { "content": "To you my good sir!" }] }
You can also provide other locals to the partial you're rendering to.
# Provide the `include_body` local to the partial when rendering a single object
json.post @post, partial: 'posts/post', as: :post, include_body: true
# Provide a local to the partial when rendering a collection.
# Each item in the collection will render with `include_author: true`.
json.comments @post.comments, partial: 'comments/comment', as: :comment, include_author: true
The as: :some_symbol is used with partials. It will take care of mapping the passed in object to a variable for the
partial. If the value is a collection either implicitly or explicitly by using the collection: option, then each
value of the collection is passed to the partial as the variable some_symbol. If the value is a singular object,
then the object is passed to the partial as the variable some_symbol.
Be sure not to confuse the as: option to mean nesting of the partial. For example:
# Use the default `views/comments/_comment.json.jbuilder`, putting @comment as the comment local variable.
# Note, `comment` attributes are "inlined".
json.partial! @comment, as: :comment
is quite different from:
# comment attributes are nested under a "comment" property
json.comment do
json.partial! "/comments/comment.json.jbuilder", comment: @comment
end
You can pass any objects into partial templates with or without :locals option.
json.partial! 'sub_template', locals: { user: user }
# or
json.partial! 'sub_template', user: user
Null Values
You can explicitly make Jbuilder object return null if you want:
json.extract! @post, :id, :title, :content, :published_at
json.author do
if @post.anonymous?
json.null! # or json.nil!
else
json.first_name @post.author_first_name
json.last_name @post.author_last_name
end
end
To prevent Jbuilder from including null values in the output, you can use the ignore_nil! method:
Fragment caching is supported, it uses Rails.cache and works like caching in
HTML templates:
json.cache! ['v1', @person], expires_in: 10.minutes do
json.extract! @person, :name, :age
end
You can also conditionally cache a block by using cache_if! like this:
json.cache_if! !admin?, ['v1', @person], expires_in: 10.minutes do
json.extract! @person, :name, :age
end
Aside from that, the :cached options on collection rendering is available on Rails >= 6.0. This will cache the
rendered results effectively using the multi fetch feature.
If your collection cache depends on multiple sources (try to avoid this to keep things simple), you can name all these dependencies as part of a block that returns an array:
You can set this globally with the class method key_format (from inside your
environment.rb for example):
Jbuilder.key_format camelize: :lower
By default, key format is not applied to keys of hashes that are
passed to methods like set!, array! or merge!. You can opt into
deeply transforming these as well:
You can set this globally with the class method deep_format_keys (from inside your
environment.rb for example):
Jbuilder.deep_format_keys true
Testing JBuilder Response body with RSpec
To test the response body of your controller spec, enable render_views in your RSpec context. This configuration renders the views in a controller test.
Contributing to Jbuilder
Jbuilder is the work of many contributors. You're encouraged to submit pull requests, propose
features and discuss issues.
No homepage URL was recorded for rails/jbuilder in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
Does rails/jbuilder have any tags?
TopGit's last sync did not record any GitHub topics for rails/jbuilder. GitHub topics appear in the right sidebar of a repository page; that's the authoritative place to check.
How active is development on rails/jbuilder?
The most recent commit recorded on rails/jbuilder was 2 months ago, based on the GitHub push timestamp. The repository has 454 forks — one of the better signals of community interest.
How many stars does rails/jbuilder have?
rails/jbuilder has 4.4k GitHub stars — refresh the page for the live number, or check github.com/rails/jbuilder. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
What language is rails/jbuilder written in?
rails/jbuilder is written primarily in Ruby. GitHub's language field is based on the largest share of bytes in the default branch.
What license does rails/jbuilder use?
rails/jbuilder is released under the MIT license. Always verify the LICENSE file directly on GitHub for the authoritative terms — license strings can be edited out of sync with a project's actual stance.
Where do I read more about rails/jbuilder?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/rails/jbuilder is the definitive source.
Read full README in the tab above.
Want a second opinion on jbuilder?
Ask an AI that can read this page — one click and you get its take on jbuilder.