A summary of data about the Ruby ecosystem.

https://github.com/ruby/json

JSON implementation for Ruby
https://github.com/ruby/json

Keywords

json json-parser ruby

Keywords from Contributors

rubygems activerecord activejob mvc rack documentation-tool rspec ruby-gem sinatra rubocop

Last synced: about 15 hours ago
JSON representation

Repository metadata

JSON implementation for Ruby

README.md

JSON implementation for Ruby

CI

Description

This is an implementation of the JSON specification according to RFC 7159
http://www.ietf.org/rfc/rfc7159.txt .

The JSON generator generate UTF-8 character sequences by default.
If an :ascii_only option with a true value is given, they escape all
non-ASCII and control characters with \uXXXX escape sequences, and support
UTF-16 surrogate pairs in order to be able to generate the whole range of
unicode code points.

All strings, that are to be encoded as JSON strings, should be UTF-8 byte
sequences on the Ruby side.

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add json

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install json

Basic Usage

To use JSON you can

require 'json'

Now you can parse a JSON document into a ruby data structure by calling

JSON.parse(document)

If you want to generate a JSON document from a ruby data structure call

JSON.generate(data)

You can also use the pretty_generate method (which formats the output more
verbosely and nicely).

Casting non native types

JSON documents can only support Hashes, Arrays, Strings, Integers and Floats.

By default if you attempt to serialize something else, JSON.generate will
search for a #to_json method on that object:

Position = Struct.new(:latitude, :longitude) do
  def to_json(state = nil, *)
    JSON::State.from_state(state).generate({
      latitude: latitude,
      longitude: longitude,
    })
  end
end

JSON.generate([
  Position.new(12323.234, 435345.233),
  Position.new(23434.676, 159435.324),
]) # => [{"latitude":12323.234,"longitude":435345.233},{"latitude":23434.676,"longitude":159435.324}]

If a #to_json method isn't defined on the object, JSON.generate will fallback to call #to_s:

JSON.generate(Object.new) # => "#<Object:0x000000011e768b98>"

Both of these behavior can be disabled using the strict: true option:

JSON.generate(Object.new, strict: true) # => Object not allowed in JSON (JSON::GeneratorError)
JSON.generate(Position.new(1, 2), strict: true) # => Position not allowed in JSON (JSON::GeneratorError)

JSON::Coder

Since #to_json methods are global, it can sometimes be problematic if you need a given type to be
serialized in different ways in different locations.

Instead it is recommended to use the newer JSON::Coder API:

module MyApp
  API_JSON_CODER = JSON::Coder.new do |object, is_object_key|
    case object
    when Time
      object.iso8601(3)
    else
      object
    end
  end
end

puts MyApp::API_JSON_CODER.dump(Time.now.utc) # => "2025-01-21T08:41:44.286Z"

The provided block is called for all objects that don't have a native JSON equivalent, and
must return a Ruby object that has a native JSON equivalent.

It is also called for objects that do have a JSON equivalent, but are used as Hash keys, for instance { 1 => 2},
as well as for strings that aren't valid UTF-8:

coder = JSON::Coder.new do |object, is_object_key|
  case object
  when String
    if !object.valid_encoding? || object.encoding != Encoding::UTF_8
      Base64.encode64(object)
    else
      object
    end
  else
    object
  end
end

Combining JSON fragments

To combine JSON fragments into a bigger JSON document, you can use JSON::Fragment:

posts_json = cache.fetch_multi(post_ids) do |post_id|
  JSON.generate(Post.find(post_id))
end
posts_json.map! { |post_json| JSON::Fragment.new(post_json) }
JSON.generate({ posts: posts_json, count: posts_json.count })

Pretty Printing

JSON.generate always creates the shortest possible string representation of a
ruby data structure in one line. This is good for data storage or network
protocols, but not so good for humans to read. Fortunately there's also
JSON.pretty_generate that creates a more readable
output:

 puts JSON.pretty_generate([1, 2, {"a"=>3.141}, false, true, nil, 4..10])
 [
   1,
   2,
   {
     "a": 3.141
   },
   false,
   true,
   null,
   {
     "json_class": "Range",
     "data": [
       4,
       10,
       false
     ]
   }
 ]

Security

When parsing or serializing untrusted input, parser and generator options should never be user controlled.

# Dangerous, DO NOT DO THIS.
JSON.generate(params[:data], params[:options])

Security vulnerability reports relying on attacker controlled parsing or generator options will be handled as regular bug fixes.

Development

Prerequisites

  1. Clone the repository
  2. Install dependencies with bundle install

Testing

The full test suite can be run with:

bundle exec rake test

Release

Update the lib/json/version.rb file.

rbenv shell 2.6.5
rake build
gem push pkg/json-2.3.0.gem

rbenv shell jruby-9.2.9.0
rake build
gem push pkg/json-2.3.0-java.gem

Author

Florian Frank mailto:flori@ping.de

License

Ruby License, see https://www.ruby-lang.org/en/about/license.txt.

Download

The latest version of this library can be downloaded at

Online Documentation should be located at


Owner metadata


GitHub Events

Total
Last Year

Committers metadata

Last synced: 1 day ago

Total Commits: 1,578
Total Committers: 132
Avg Commits per committer: 11.955
Development Distribution Score (DDS): 0.714

Commits in past year: 302
Committers in past year: 34
Avg Commits per committer in past year: 8.882
Development Distribution Score (DDS) in past year: 0.401

Name Email Commits
Jean Boussier j****r@g****m 451
Florian Frank f****i@p****e 415
Hiroshi SHIBATA h****t@r****g 119
Nobuyoshi Nakada n****u@r****g 88
Étienne Barrié e****e@g****m 45
Scott Myron s****n@g****m 38
Charles Oliver Nutter h****s@h****m 38
Yusuke Endoh m****e@r****g 26
Benoit Daloze e****p@g****m 25
Kenta Murata m****n@m****p 18
BurdetteLamar b****r@y****m 17
NARUSE, Yui n****e@a****p 14
Zachary Scott e@z****o 13
Marc-Andre Lafortune g****b@m****a 10
Vipul A M v****d@g****m 10
ydah t****2@g****m 10
Aaron Patterson t****e@r****g 10
kares s****f@k****g 9
Watson w****8@g****m 9
tompng t****n@g****m 8
Daniel Luz d****v@m****m 7
Michael Mac-Vicar m****r@g****m 6
Takashi Kokubun t****n@g****m 5
Sho Hashimoto s****t@g****m 5
Masataka Pocke Kuwabara k****a@p****e 4
Olle Jonsson o****n@g****m 4
Per Lundberg p****g@e****m 4
Peter Zhu p****r@p****a 4
GrantBirki g****e@g****m 4
eno e****o@o****g 4
and 102 more...

Committer domains:


Issue and Pull Request metadata

Last synced: 1 day ago

Total issues: 220
Total pull requests: 567
Average time to close issues: over 2 years
Average time to close pull requests: about 2 months
Total issue authors: 174
Total pull request authors: 81
Average comments per issue: 3.69
Average comments per pull request: 1.67
Merged pull request: 450
Bot issues: 0
Bot pull requests: 3

Past year issues: 28
Past year pull requests: 163
Past year average time to close issues: 15 days
Past year average time to close pull requests: 2 days
Past year issue authors: 21
Past year pull request authors: 35
Past year average comments per issue: 5.18
Past year average comments per pull request: 1.37
Past year merged pull request: 142
Past year bot issues: 0
Past year bot pull requests: 3

More stats: https://issues.ecosyste.ms/repositories/lookup?url=https://github.com/ruby/json

Top Issue Authors

  • headius (9)
  • mperham (7)
  • byroot (7)
  • kou (3)
  • mernen (3)
  • nurse (3)
  • myronmarston (3)
  • eregon (3)
  • HoneyryderChuck (3)
  • Watson1978 (3)
  • nobu (2)
  • marcandre (2)
  • forthrin (2)
  • hsbt (2)
  • ghost (2)

Top Pull Request Authors

  • byroot (156)
  • casperisfine (95)
  • samyron (37)
  • hsbt (35)
  • nobu (30)
  • etiennebarrie (25)
  • eregon (21)
  • radiospiel (12)
  • tompng (10)
  • ydah (10)
  • headius (10)
  • mame (10)
  • GrantBirki (6)
  • k0kubun (5)
  • pocke (4)

Top Issue Labels

  • bug (15)
  • fixed (11)
  • new feature (9)
  • jruby (7)
  • rfc (3)
  • worksforme (3)
  • 3rd party bug (1)
  • duplicate (1)

Top Pull Request Labels

  • github_actions (3)
  • dependencies (3)
  • documentation (2)
  • new feature (2)
  • bug (2)
  • fixed (1)

Package metadata

gem.coop: json

This is a JSON implementation as a Ruby extension in C.

gem.coop: json_pure

This is a JSON implementation in pure Ruby.

  • Homepage: https://ruby.github.io/json
  • Documentation: http://www.rubydoc.info/gems/json_pure/
  • Licenses: Ruby
  • Latest release: 2.8.1 (published almost 2 years ago)
  • Last Synced: 2026-10-04T02:30:37.183Z (1 day ago)
  • Versions: 82
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 71,668,807 Total
  • Docker Downloads: 549,489,576
  • Rankings:
    • Dependent repos count: 0.0%
    • Dependent packages count: 0.0%
    • Average: 0.147%
    • Docker downloads count: 0.179%
    • Downloads: 0.408%
  • Maintainers (5)
debian-13: ruby-json

  • Homepage: https://ruby.github.io/json
  • Documentation: https://packages.debian.org/trixie/ruby-json
  • Licenses: other
  • Latest release: 2.9.1+dfsg-1 (published 8 months ago)
  • Last Synced: 2026-03-14T18:10:32.321Z (7 months ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Rankings:
    • Dependent repos count: 0.0%
    • Dependent packages count: 0.0%
    • Average: 0.205%
    • Forks count: 0.268%
    • Stargazers count: 0.55%
rubygems.org: json

This is a JSON implementation as a Ruby extension in C.

rubygems.org: json_pure

This is a JSON implementation in pure Ruby.

  • Homepage: https://ruby.github.io/json
  • Documentation: http://www.rubydoc.info/gems/json_pure/
  • Licenses: Ruby
  • Latest release: 2.8.1 (published almost 2 years ago)
  • Last Synced: 2026-10-04T02:30:37.140Z (1 day ago)
  • Versions: 82
  • Dependent Packages: 599
  • Dependent Repositories: 28,645
  • Downloads: 71,668,807 Total
  • Docker Downloads: 549,489,576
  • Rankings:
    • Dependent packages count: 0.076%
    • Dependent repos count: 0.21%
    • Docker downloads count: 0.232%
    • Downloads: 0.361%
    • Average: 0.773%
    • Forks count: 1.496%
    • Stargazers count: 2.261%
  • Maintainers (5)
proxy.golang.org: github.com/ruby/json

  • Homepage:
  • Documentation: https://pkg.go.dev/github.com/ruby/json#section-documentation
  • Licenses: other
  • Latest release: v3.0.2+incompatible (published 26 days ago)
  • Last Synced: 2026-10-04T02:30:37.522Z (1 day ago)
  • Versions: 114
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Rankings:
    • Dependent packages count: 5.442%
    • Average: 5.624%
    • Dependent repos count: 5.807%
rubygems.org: ed-precompiled_json

This is a JSON implementation as a Ruby extension in C.

  • Homepage: https://github.com/ruby/json
  • Documentation: http://www.rubydoc.info/gems/ed-precompiled_json/
  • Licenses: Ruby
  • Latest release: 2.15.1 (published 12 months ago)
  • Last Synced: 2026-10-04T02:30:35.929Z (1 day ago)
  • Versions: 3
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 1,438 Total
  • Rankings:
    • Forks count: 1.459%
    • Stargazers count: 2.129%
    • Dependent packages count: 14.231%
    • Average: 30.696%
    • Dependent repos count: 43.59%
    • Downloads: 92.073%
  • Maintainers (1)
gem.coop: json-clone

This is a JSON implementation as a Ruby extension in C.

  • Homepage: https://github.com/ruby/json
  • Status: removed
  • Documentation: http://www.rubydoc.info/gems/json-clone/
  • Licenses: Ruby
  • Latest release: 2.20.0 (published 3 months ago)
  • Last Synced: 2026-10-04T02:30:35.334Z (1 day ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 916 Total
  • Rankings:
    • Dependent repos count: 0.0%
    • Dependent packages count: 0.0%
    • Average: 32.918%
    • Downloads: 98.754%
  • Maintainers (1)
gem.coop: json-extended

This is a JSON implementation as a Ruby extension in C.

  • Homepage: https://github.com/ruby/json
  • Status: removed
  • Documentation: http://www.rubydoc.info/gems/json-extended/
  • Licenses: Ruby
  • Latest release: 2.20.0 (published 3 months ago)
  • Last Synced: 2026-10-04T02:30:35.959Z (1 day ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 906 Total
  • Rankings:
    • Dependent repos count: 0.0%
    • Dependent packages count: 0.0%
    • Average: 32.968%
    • Downloads: 98.904%
  • Maintainers (1)
gem.coop: json_pack

This is a JSON implementation as a Ruby extension in C.

  • Homepage: https://github.com/ruby/json
  • Status: removed
  • Documentation: http://www.rubydoc.info/gems/json_pack/
  • Licenses: Ruby
  • Latest release: 2.20.0 (published 3 months ago)
  • Last Synced: 2026-10-04T02:30:36.419Z (1 day ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 841 Total
  • Rankings:
    • Dependent repos count: 0.0%
    • Dependent packages count: 0.0%
    • Average: 33.045%
    • Downloads: 99.136%
  • Maintainers (1)
gem.coop: json_box

This is a JSON implementation as a Ruby extension in C.

  • Homepage: https://github.com/ruby/json
  • Status: removed
  • Documentation: http://www.rubydoc.info/gems/json_box/
  • Licenses: Ruby
  • Latest release: 2.20.0 (published 3 months ago)
  • Last Synced: 2026-10-04T02:30:35.878Z (1 day ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 853 Total
  • Rankings:
    • Dependent repos count: 0.0%
    • Dependent packages count: 0.0%
    • Average: 33.08%
    • Downloads: 99.241%
  • Maintainers (1)
gem.coop: json_lite

This is a JSON implementation as a Ruby extension in C.

  • Homepage: https://github.com/ruby/json
  • Status: removed
  • Documentation: http://www.rubydoc.info/gems/json_lite/
  • Licenses: Ruby
  • Latest release: 2.20.0 (published 3 months ago)
  • Last Synced: 2026-10-04T02:30:37.150Z (1 day ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 782 Total
  • Rankings:
    • Dependent repos count: 0.0%
    • Dependent packages count: 0.0%
    • Average: 33.114%
    • Downloads: 99.341%
  • Maintainers (1)
gem.coop: ed-precompiled_json

This is a JSON implementation as a Ruby extension in C.

  • Homepage: https://github.com/ruby/json
  • Documentation: http://www.rubydoc.info/gems/ed-precompiled_json/
  • Licenses: Ruby
  • Latest release: 2.15.1 (published 12 months ago)
  • Last Synced: 2026-10-04T02:30:36.280Z (1 day ago)
  • Versions: 3
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 1,438 Total
  • Rankings:
    • Dependent repos count: 0.0%
    • Dependent packages count: 0.0%
    • Average: 33.154%
    • Downloads: 99.463%
  • Maintainers (1)
rubygems.org: json-clone

This is a JSON implementation as a Ruby extension in C.

  • Homepage: https://github.com/ruby/json
  • Status: removed
  • Documentation: http://www.rubydoc.info/gems/json-clone/
  • Licenses: Ruby
  • Latest release: 2.20.0 (published 3 months ago)
  • Last Synced: 2026-10-04T02:30:35.339Z (1 day ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 916 Total
  • Rankings:
    • Dependent packages count: 13.672%
    • Dependent repos count: 41.876%
    • Average: 49.126%
    • Downloads: 91.829%
  • Maintainers (1)
rubygems.org: json-extended

This is a JSON implementation as a Ruby extension in C.

  • Homepage: https://github.com/ruby/json
  • Status: removed
  • Documentation: http://www.rubydoc.info/gems/json-extended/
  • Licenses: Ruby
  • Latest release: 2.20.0 (published 3 months ago)
  • Last Synced: 2026-10-04T02:30:35.897Z (1 day ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 906 Total
  • Rankings:
    • Dependent packages count: 13.672%
    • Dependent repos count: 41.876%
    • Average: 49.172%
    • Downloads: 91.967%
  • Maintainers (1)
rubygems.org: json_pack

This is a JSON implementation as a Ruby extension in C.

  • Homepage: https://github.com/ruby/json
  • Status: removed
  • Documentation: http://www.rubydoc.info/gems/json_pack/
  • Licenses: Ruby
  • Latest release: 2.20.0 (published 3 months ago)
  • Last Synced: 2026-10-04T02:30:36.171Z (1 day ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 841 Total
  • Rankings:
    • Dependent packages count: 13.672%
    • Dependent repos count: 41.876%
    • Average: 49.245%
    • Downloads: 92.185%
  • Maintainers (1)
rubygems.org: json_box

This is a JSON implementation as a Ruby extension in C.

  • Homepage: https://github.com/ruby/json
  • Status: removed
  • Documentation: http://www.rubydoc.info/gems/json_box/
  • Licenses: Ruby
  • Latest release: 2.20.0 (published 3 months ago)
  • Last Synced: 2026-10-04T02:30:36.698Z (1 day ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 853 Total
  • Rankings:
    • Dependent packages count: 13.672%
    • Dependent repos count: 41.876%
    • Average: 49.264%
    • Downloads: 92.244%
  • Maintainers (1)
rubygems.org: json_lite

This is a JSON implementation as a Ruby extension in C.

  • Homepage: https://github.com/ruby/json
  • Status: removed
  • Documentation: http://www.rubydoc.info/gems/json_lite/
  • Licenses: Ruby
  • Latest release: 2.20.0 (published 3 months ago)
  • Last Synced: 2026-10-04T02:30:37.181Z (1 day ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 782 Total
  • Rankings:
    • Dependent packages count: 13.672%
    • Dependent repos count: 41.876%
    • Average: 49.274%
    • Downloads: 92.274%
  • Maintainers (1)
gentoo-portage: dev-ruby/json

A JSON implementation as a Ruby extension

  • Homepage: https://github.com/ruby/json
  • Documentation: https://packages.gentoo.org/packages/dev-ruby/json
  • Licenses: || ( BSD-2 Ruby )
  • Latest release: 2.19.4 (published 5 months ago)
  • Last Synced: 2026-07-30T01:31:31.643Z (2 months ago)
  • Versions: 9
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Rankings:
    • Dependent repos count: 0.0%
    • Dependent packages count: 0.0%
    • Average: 100%

Dependencies

Gemfile rubygems
  • all_images ~> 0
  • rake >= 0
  • test-unit >= 0
.github/workflows/ci.yml actions
  • actions/checkout v2 composite
  • ruby/setup-ruby v1 composite
json.gemspec rubygems
.github/workflows/sync-ruby.yml actions
  • actions/checkout v6 composite
  • actions/create-github-app-token v2 composite
  • convictional/trigger-workflow-and-wait v1.6.5 composite

Score: 34.920901569117646