jbuilder

https://github.com/rails/jbuilder

Ruby

Jbuilder: generate JSON objects with a Builder-style DSL

Jbuilder#key_format!

Specifies formatting to be applied to the key. Passing in a name of a function
will cause that function to be called on the key.  So :upcase will upper case
the key.  You can also pass in lambdas for more complex transformations.

Example:

  json.key_format! :upcase
  json.author do
    json.name "David"
    json.age 32
  end

  { "AUTHOR": { "NAME": "David", "AGE": 32 } }

You can pass parameters to the method using a hash pair.

  json.key_format! camelize: :lower
  json.first_name "David"

  { "firstName": "David" }

Lambdas can also be used.

  json.key_format! ->(key){ "_" + key }
  json.first_name "David"

  { "_first_name": "David" }

Source | Google | Stack overflow

Edit

git clone [email protected]:rails/jbuilder.git

cd jbuilder

open lib/jbuilder.rb

Contribute

# Make a new branch

git checkout -b -your-name--update-docs-Jbuilder-key_format--for-pr


# Commit to git

git add lib/jbuilder.rbgit commit -m "better docs for Jbuilder#key_format!"


# Open pull request

gem install hub # on a mac you can `brew install hub`

hub fork

git push <your name> -your-name--update-docs-Jbuilder-key_format--for-pr

hub pull-request


# Celebrate!