Six .json.jbuilder views, already shipping in this app

Free preview 2 min to read

A model dumped raw

Catalog::Course.find_by(slug: "computer-architecture").to_json

On a development database, that returns:

{"id":1,"active":true,"category_id":12,"created_at":"2026-09-19T21:42:45.504Z","description":"Understanding CPU, RAM, and disk in a Ruby context.","level":"intern","position":1,"slug":"computer-architecture","title":"Computer architecture","updated_at":"2026-09-19T22:32:26.280Z","title_en":"Computer architecture","title_pt_br":"Arquitetura de computadores","description_en":"Understanding CPU, RAM, and disk in a Ruby context.","description_pt_br":"Entendendo CPU, RAM e disco no contexto Ruby."}

Fourteen keys: every column on courses, category_id included, plus both locale variants of title and description, in whatever order ActiveRecord produces them. Compare what this app's catalog/search/index.json.jbuilder view sends back for the same course, as one search result among several: {"title":"Computer architecture","url":"/catalog/courses/computer-architecture","type":"course","level":"intern"}. Four keys, chosen on purpose, in the order the file writes them. Same row, two different JSON shapes: that difference is what this course is about.

Installed, already in the Gemfile

# Gemfile, line 20
gem "jbuilder"

Gemfile.lock pins it at jbuilder (2.15.1), and this app has six views that use it. Two of them get read in full in this course: app/views/execution/api/v1/sandbox/execute.json.jbuilder and app/views/catalog/search/index.json.jbuilder. The other four, the sandbox's submissions/show.json.jbuilder and three partner API views under app/views/partners/api/v1/, come up where they show a form the first two don't use. A .json.jbuilder file is a view, found and rendered like an .html.erb file (the same app/views/<controller>/<action> folder convention). Instead of building a string of HTML, it builds a JSON value through an object every Jbuilder view receives, called json.

What json is

json is an instance of JbuilderTemplate, a subclass of the gem's Jbuilder class, created before your template's code runs. Each method call on it, such as json.status "pending" or json.array! ..., either sets a key on a JSON object being built or turns the whole thing into a JSON array. The syntax isn't special Ruby: it's ordinary method calls on an ordinary object, which is why the next lessons can read this app's files line by line and explain what each call does.

What this course covers

Seven lessons, each checked against this app's files, its database or Jbuilder's installed source (jbuilder-2.15.1, found with bundle show jbuilder). Lesson 2 covers the implicit setter behind json.status "pending". Lesson 3 covers json.array!, through the search view's mix of courses and lessons. Lesson 4 covers json.partial!, installed and documented, and why none of this app's views needs it yet. Lesson 5 covers how Rails finds a .json.jbuilder file for a request. Lesson 6 measures the difference between shaping JSON this way and calling .to_json or .as_json on a model.

Lesson 1 of 7 · free

Keep going with lesson 2

json.key value is set!, in the sandbox view

Shows how implicit calls such as json.status reach set! through method_missing in the sandbox execute view.

A free account also gives you the quizzes, your progress and notes, and the discussion.

Type to start searching...