{"id":13747680,"url":"https://github.com/dewski/json_builder","last_synced_at":"2025-05-09T09:30:41.819Z","repository":{"id":1037583,"uuid":"866611","full_name":"dewski/json_builder","owner":"dewski","description":"Rails provides an excellent XML Builder by default to build RSS and ATOM feeds, but nothing to help you build complex and custom JSON data structures. JSON Builder is here to help.","archived":true,"fork":false,"pushed_at":"2016-11-05T00:23:10.000Z","size":753,"stargazers_count":243,"open_issues_count":14,"forks_count":49,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-28T02:05:19.684Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://garrettbjerkhoel.com/json_builder/","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dewski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-08-27T16:00:55.000Z","updated_at":"2024-09-21T18:10:06.000Z","dependencies_parsed_at":"2022-08-06T10:15:07.482Z","dependency_job_id":null,"html_url":"https://github.com/dewski/json_builder","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dewski%2Fjson_builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dewski%2Fjson_builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dewski%2Fjson_builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dewski%2Fjson_builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dewski","download_url":"https://codeload.github.com/dewski/json_builder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253226288,"owners_count":21874308,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T06:01:37.417Z","updated_at":"2025-05-09T09:30:41.554Z","avatar_url":"https://github.com/dewski.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"*No longer maintained, please check out [jbuilder](https://github.com/rails/jbuilder)*\n\nJSON Builder [![Build Status](https://secure.travis-ci.org/dewski/json_builder.png)](http://travis-ci.org/dewski/json_builder)\n============\nRails provides an excellent XML Builder by default to build RSS and ATOM feeds, but nothing to help you build complex and custom JSON data structures. The standard `to_json` works just fine, but can get very verbose when you need full control of what is generated and performance is a factor. JSON Builder hopes to solve that problem.\n\n## Sample Usage\n\n```ruby\nrequire 'json_builder'\n\njson = JSONBuilder::Compiler.generate do\n  name 'Garrett Bjerkhoel'\n  email 'spam@garrettbjerkhoel.com'\n  url user_url(user)\n  address do\n    street '1234 1st Ave'\n    city 'New York'\n    state 'NY'\n    zip 10065\n  end\n  key :nil, 'testing a custom key name'\n  skills do\n    ruby true\n    asp false\n  end\n  longstring do\n    # Could be a highly intensive process that only returns a string\n    '12345' * 25\n  end\nend\n```\n\nWhich will generate:\n\n```json\n{\n  \"name\": \"Garrett Bjerkhoel\",\n  \"email\": \"spam@garrettbjerkhoel.com\",\n  \"url\": \"http://examplesite.com/dewski\",\n  \"address\": {\n    \"street\": \"1234 1st Ave\",\n    \"city\": \"New York\",\n    \"state\": \"NY\",\n    \"zip\": 10065\n  },\n  \"nil\": \"testing a custom key name\",\n  \"skills\": {\n    \"ruby\": true,\n    \"asp\": false\n  },\n  \"longstring\": \"1234512345123451234512345...\"\n}\n```\n\nIf you'd like to just generate an array:\n\n```ruby\narray ['Garrett Bjerkhoel', 'John Doe'] do |name|\n  first, last = name.split(' ')\n  first first\n  last last\nend\n```\n\nWhich will output the following:\n\n```json\n[\n  {\n    \"first\": \"Garrett\",\n    \"last\": \"Bjerkhoel\"\n  },\n  {\n    \"first\": \"John\",\n    \"last\": \"Doe\"\n  }\n]\n```\n\nJust a note, if you use an array block, all other builder methods will be ignored.\n\n## Using JSON Builder with Rails\nFirst, make sure to add the gem to your `Gemfile`.\n\n```ruby\ngem 'json_builder'\n```\n\nSecond, make sure your controller responds to `json`:\n\n```ruby\nclass UsersController \u003c ApplicationController\n  respond_to :json\n  \n  def index\n    @users = User.order('id DESC').page(params[:page]).per(2)\n    respond_with @users\n  end\nend\n```\n\nLastly, create `app/views/users/index.json.json_builder` which could look something like:\n    \n```ruby\ncount @users.count\npage @users.current_page\nper_page @users.per_page\npages_count @users.num_pages\nresults @users do |user|\n  id user.id\n  name user.name\n  body user.body\n  url user_url(user)\n  links user.links do |link|\n    url link.url\n    visits link.visits\n    last_visited link.last_visited\n  end\nend\n```\n\nYou will get something like:\n\n```json\n{\n  \"count\": 10,\n  \"page\": 1,\n  \"per_page\": 2,\n  \"pages_count\": 5,\n  \"results\": [\n    {\n      \"id\": 1,\n      \"name\": \"Garrett Bjerkhoel\",\n      \"body\": \"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod.\",\n      \"url\": \"http://example.com/users/garrett-bjerkhoel\",\n      \"links\": [\n        {\n          \"url\": \"http://github.com/\",\n          \"visits\": 500,\n          \"last_visited\": \"2011-11-271T00:00:01Z\"\n        },\n        {\n          \"url\": \"http://garrettbjerkhoel.com/\",\n          \"visits\": 1500,\n          \"last_visited\": \"2011-11-261T00:00:01Z\"\n        }\n      ]\n    }, {\n      \"id\": 2,\n      \"name\": \"John Doe\",\n      \"body\": \"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod.\",\n      \"url\": \"http://example.com/users/john-doe\",\n      \"links\": [\n        {\n          \"url\": \"http://google.com/\",\n          \"visits\": 11000,\n          \"last_visited\": \"2010-05-221T00:00:01Z\"\n        },\n        {\n          \"url\": \"http://twitter.com/\",\n          \"visits\": 155012857,\n          \"last_visited\": \"2011-11-261T00:00:01Z\"\n        }\n      ]\n    }\n  ]\n}\n```\n\n### Including JSONP callbacks\n\nOut of the box JSON Builder supports JSONP callbacks when used within a Rails project just by using the `callback` parameter. For instance, if you requested `/users.json?callback=myjscallback`, you'll get a callback wrapping the response:\n\n```json\nmyjscallback([\n  {\n    \"name\": \"Garrett Bjerkhoel\"\n  },\n  {\n    \"name\": \"John Doe\"\n  }\n])\n```\n\nTo turn off JSONP callbacks globally or just per-environment:\n\n\n#### Globally\n\n```ruby\nActionView::Base.json_callback = false\n```\n\n#### Per Environment\n\n```ruby\nSample::Application.configure do\n  config.action_view.json_callback = false\nend\n```\n\n### Pretty Print Output\n\nOut of the box JSON Builder supports pretty printing only during development, it's disabled by default in other environments for performance. If you'd like to enable or disable pretty printing you can do it within your environment file or you can do it globally.\n\nWith pretty print on:\n\n```json\n{\n  \"name\": \"Garrett Bjerkhoel\",\n  \"email\": \"spam@garrettbjerkhoel.com\"\n}\n```\n\nWithout:\n\n```json\n{\"name\": \"Garrett Bjerkhoel\", \"email\": \"spam@garrettbjerkhoel.com\"}\n```\n\n#### Per Environment\n\n```ruby\nSample::Application.configure do\n  config.action_view.pretty_print_json = false\nend\n```\n\n#### Globally\n\n```ruby\nActionView::Base.pretty_print_json = false\n```\n\n## Speed\nJSON Builder is very fast, it's roughly 3.6 times faster than the core XML Builder based on the [speed benchmark](http://github.com/dewski/json_builder/blob/master/spec/benchmarks/builder.rb).\n\n                 user       system      total       real\n    JSONBuilder  2.950000   0.010000    2.960000    (2.968790)\n        Builder  10.820000  0.040000    10.860000   (10.930497)\n\n## Alternative libraries\n\nThere are alternatives to JSON Builder, each good in their own way with different API's and design approaches that are worth checking out. Although, I would love to hear why JSON Builder didn't fit your needs, by [message or issue.\n\n * [jbuilder](https://github.com/rails/jbuilder)\n * [RABL](https://github.com/nesquena/rabl)\n * [Tequila](https://github.com/inem/tequila)\n * [Argonaut](https://github.com/jbr/argonaut)\n * [Jsonify](https://github.com/bsiggelkow/jsonify)\n * [RepresentationView](https://github.com/mdub/representative_view)\n\n## Note on Patches/Pull Requests\n\n- Fork the project.\n- Make your feature addition or bug fix.\n- Add tests for it. This is important so I don't break it in a future version unintentionally.\n- Commit, do not mess with Rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself in another branch so I can ignore when I pull)\n- Send me a pull request. Bonus points for topic branches.\n\n## Copyright\nCopyright © 2012 Garrett Bjerkhoel. See [MIT-LICENSE](http://github.com/dewski/json_builder/blob/master/MIT-LICENSE) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdewski%2Fjson_builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdewski%2Fjson_builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdewski%2Fjson_builder/lists"}