Pseudo-Hippy
Pseudo-Hippy

Reputation: 197

How can I change the name of a key using jbuilder

I have an index.json.jbuilder which looks like this:

json.results do |result|
    json.array! @artists do |artist|
      json.extract! artist, :id, :name
    end
  end

This produces the follow JSON format:

{"results": [{"id": 2, "name": "A-Austr"},{"id": 3,"name": "Abacus"}]}

I want to change the name of the key "name" to "text". Is there way to do this in the jbuilder? I don't want to change the name of the field in the database and would prefer not to have to do this using JS once the data is received. Thanks!!

Upvotes: 1

Views: 1298

Answers (1)

max pleaner
max pleaner

Reputation: 26768

json.extract! artist, :id, :name is essentially the same thing as:

json.id artist.id
json.name artist.name

You can replace json.name with json.text for your use-case.

Upvotes: 1

Related Questions