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 libyaml
Last synced: about 24 hours ago
JSON representation
Repository metadata
JSON implementation for Ruby
- Host: GitHub
- URL: https://github.com/ruby/json
- Owner: ruby
- License: other
- Created: 2009-08-24T22:21:39.000Z (over 16 years ago)
- Default Branch: master
- Last Pushed: 2026-04-20T12:56:23.000Z (10 days ago)
- Last Synced: 2026-04-24T19:04:14.768Z (6 days ago)
- Topics: json, json-parser, ruby
- Language: Ruby
- Homepage: https://docs.ruby-lang.org/en/master/JSON.html
- Size: 5.42 MB
- Stars: 743
- Watchers: 55
- Forks: 366
- Open Issues: 7
- Releases: 105
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: COPYING
README.md
JSON implementation for Ruby
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. To encode raw binary strings, that aren't UTF-8
encoded, please use the to_json_raw_object method of String (which produces
an object, that contains a byte array) and decode the result on the receiving
endpoint.
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) or fast_generate (which doesn't do any of the security
checks generate performs, e. g. nesting deepness checks).
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)) # => 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::Combining.new do |object, is_object_key|
case object
when String
if !string.valid_encoding? || string.encoding != Encoding::UTF_8
Base64.encode64(string)
else
string
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 })
Round-tripping arbitrary types
[!CAUTION]
You should never useJSON.unsafe_loadnorJSON.parse(str, create_additions: true)to parse untrusted user input,
as it can lead to remote code execution vulnerabilities.
To create a JSON document from a ruby data structure, you can call
JSON.generate like that:
json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
# => "[1,2,{\"a\":3.141},false,true,null,\"4..10\"]"
To get back a ruby data structure from a JSON document, you have to call
JSON.parse on it:
JSON.parse json
# => [1, 2, {"a"=>3.141}, false, true, nil, "4..10"]
Note, that the range from the original data structure is a simple
string now. The reason for this is, that JSON doesn't support ranges
or arbitrary classes. In this case the json library falls back to call
Object#to_json, which is the same as #to_s.to_json.
It's possible to add JSON support serialization to arbitrary classes by
simply implementing a more specialized version of the #to_json method, that
should return a JSON object (a hash converted to JSON with #to_json) like
this (don't forget the *a for all the arguments):
class Range
def to_json(*a)
{
'json_class' => self.class.name, # = 'Range'
'data' => [ first, last, exclude_end? ]
}.to_json(*a)
end
end
The hash key json_class is the class, that will be asked to deserialise the
JSON representation later. In this case it's Range, but any namespace of
the form A::B or ::A::B will do. All other keys are arbitrary and can be
used to store the necessary data to configure the object to be deserialised.
If the key json_class is found in a JSON object, the JSON parser checks
if the given class responds to the json_create class method. If so, it is
called with the JSON object converted to a Ruby hash. So a range can
be deserialised by implementing Range.json_create like this:
class Range
def self.json_create(o)
new(*o['data'])
end
end
Now it possible to serialise/deserialise ranges as well:
json = JSON.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]}]"
JSON.parse json
# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
json = JSON.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]}]"
JSON.unsafe_load json
# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
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 (or 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
]
}
]
There are also the methods Kernel#j for generate, and Kernel#jj for
pretty_generate output to the console, that work analogous to Core Ruby's p and
the pp library's pp methods.
Development
Prerequisites
- Clone the repository
- 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
- Name: The Ruby Programming Language
- Login: ruby
- Email: info@ruby-lang.org
- Kind: organization
- Description: Repositories related to the Ruby Programming language
- Website: https://www.ruby-lang.org/
- Location: Matsue, Japan
- Twitter:
- Company:
- Icon url: https://avatars.githubusercontent.com/u/210414?v=4
- Repositories: 171
- Last ynced at: 2023-04-09T03:40:20.875Z
- Profile URL: https://github.com/ruby
GitHub Events
Total
- Create event: 39
- Commit comment event: 6
- Release event: 24
- Delete event: 16
- Pull request event: 382
- Fork event: 31
- Issues event: 167
- Watch event: 68
- Issue comment event: 685
- Push event: 299
- Pull request review event: 168
- Pull request review comment event: 166
Last Year
- Create event: 13
- Commit comment event: 3
- Release event: 12
- Delete event: 7
- Pull request event: 125
- Fork event: 11
- Issues event: 36
- Watch event: 15
- Issue comment event: 198
- Push event: 120
- Pull request review event: 60
- Pull request review comment event: 57
Committers metadata
Last synced: 3 days ago
Total Commits: 1,419
Total Committers: 121
Avg Commits per committer: 11.727
Development Distribution Score (DDS): 0.708
Commits in past year: 266
Committers in past year: 26
Avg Commits per committer in past year: 10.231
Development Distribution Score (DDS) in past year: 0.462
| Name | Commits | |
|---|---|---|
| Florian Frank | f****i@p****e | 415 |
| Jean Boussier | j****r@g****m | 351 |
| Hiroshi SHIBATA | h****t@r****g | 119 |
| Nobuyoshi Nakada | n****u@r****g | 82 |
| Étienne Barrié | e****e@g****m | 45 |
| Charles Oliver Nutter | h****s@h****m | 38 |
| Scott Myron | s****n@g****m | 26 |
| Benoit Daloze | e****p@g****m | 23 |
| Yusuke Endoh | m****e@r****g | 19 |
| 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 |
| Aaron Patterson | t****e@r****g | 10 |
| kares | s****f@k****g | 9 |
| Daniel Luz | d****v@m****m | 7 |
| Watson | w****8@g****m | 7 |
| tompng | t****n@g****m | 7 |
| Michael Mac-Vicar | m****r@g****m | 6 |
| Takashi Kokubun | t****n@g****m | 5 |
| Sho Hashimoto | s****t@g****m | 5 |
| MSP-Greg | G****s@g****m | 4 |
| Olle Jonsson | o****n@g****m | 4 |
| Per Lundberg | p****g@e****m | 4 |
| GrantBirki | g****e@g****m | 4 |
| eno | e****o@o****g | 4 |
| Shota Fukumori (sora_h) | s****4@g****m | 4 |
| Ryan Davis | r****y@z****m | 4 |
| and 91 more... | ||
Committer domains:
- ruby-lang.org: 6
- shopify.com: 4
- me.com: 2
- software.baldauf.org: 1
- redhat.com: 1
- unth.net: 1
- newrelic.com: 1
- jeremyevans.net: 1
- jc00ke.com: 1
- suse.de: 1
- track.jonfram.net: 1
- zacharyscott.net: 1
- hawthorn.email: 1
- parabola.nu: 1
- tenjin.ca: 1
- jonathanleighton.com: 1
- mbf.nifty.com: 1
- peterzhu.ca: 1
- zenspider.com: 1
- open-lab.org: 1
- ecraft.com: 1
- mernen.com: 1
- kares.org: 1
- marc-andre.ca: 1
- zzak.io: 1
- airemix.jp: 1
- mrkn.jp: 1
- headius.com: 1
- ping.de: 1
- salimane.com: 1
- oriontransfer.co.nz: 1
- nna774.net: 1
- canonical.com: 1
- spkdev.net: 1
- shakenbu.org: 1
- atdot.net: 1
- rhe.jp: 1
- mdsol.com: 1
- joshpeek.com: 1
- glatisant.org: 1
- lostapathy.com: 1
- kaworu.ch: 1
- menfin.info: 1
- gusto.com: 1
- segment7.net: 1
- europe.com: 1
- jrits.ricoh.co.jp: 1
- engineyard.com: 1
- amd.com: 1
- us.ibm.com: 1
- gmx.net: 1
- niftybox.net: 1
- rah.im: 1
- envato.com: 1
- perham.net: 1
- vakuumverpackt.de: 1
- plentz.org: 1
Issue and Pull Request metadata
Last synced: 3 days ago
Total issues: 205
Total pull requests: 462
Average time to close issues: over 2 years
Average time to close pull requests: 2 months
Total issue authors: 165
Total pull request authors: 68
Average comments per issue: 3.5
Average comments per pull request: 1.81
Merged pull request: 351
Bot issues: 0
Bot pull requests: 2
Past year issues: 27
Past year pull requests: 153
Past year average time to close issues: 1 day
Past year average time to close pull requests: 3 days
Past year issue authors: 25
Past year pull request authors: 28
Past year average comments per issue: 4.0
Past year average comments per pull request: 1.35
Past year merged pull request: 111
Past year bot issues: 0
Past year bot pull requests: 2
Top Issue Authors
- headius (9)
- mperham (7)
- byroot (7)
- HoneyryderChuck (3)
- eregon (3)
- myronmarston (3)
- mernen (3)
- nurse (3)
- vakuum (2)
- david22swan (2)
- jterapin (2)
- Wardrop (2)
- nvasilevski (2)
- ghost (2)
- hsbt (2)
Top Pull Request Authors
- byroot (103)
- casperisfine (95)
- hsbt (34)
- samyron (31)
- nobu (25)
- etiennebarrie (25)
- eregon (19)
- radiospiel (12)
- headius (10)
- tompng (9)
- GrantBirki (6)
- k0kubun (5)
- peterzhu2118 (4)
- robinetmiller (4)
- vakuum (3)
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 (2)
- documentation (2)
- new feature (2)
- bug (2)
- dependencies (2)
- fixed (1)
Package metadata
- Total packages: 8
-
Total downloads:
- rubygems: 2,645,371,175 total
- Total docker downloads: 11,124,102,724
- Total dependent packages: 9,029 (may contain duplicates)
- Total dependent repositories: 727,455 (may contain duplicates)
- Total versions: 716
- Total maintainers: 6
- Total advisories: 8
gem.coop: json
This is a JSON implementation as a Ruby extension in C.
- Homepage: https://github.com/ruby/json
- Documentation: http://www.rubydoc.info/gems/json/
- Licenses: Ruby
- Latest release: 2.19.4 (published 12 days ago)
- Last Synced: 2026-04-26T20:01:54.752Z (4 days ago)
- Versions: 221
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 1,255,351,562 Total
- Docker Downloads: 5,012,561,786
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 0.002%
- Downloads: 0.006%
- Maintainers (5)
- Advisories:
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 over 1 year ago)
- Last Synced: 2026-04-26T20:01:52.251Z (4 days ago)
- Versions: 82
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 70,211,638 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 3 months ago)
- Last Synced: 2026-03-14T18:10:32.321Z (about 2 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.
- Homepage: https://github.com/ruby/json
- Documentation: http://www.rubydoc.info/gems/json/
- Licenses: Ruby
- Latest release: 2.19.4 (published 12 days ago)
- Last Synced: 2026-04-18T21:20:17.229Z (12 days ago)
- Versions: 222
- Dependent Packages: 8,430
- Dependent Repositories: 698,810
- Downloads: 1,249,582,551 Total
- Docker Downloads: 5,012,561,786
-
Rankings:
- Docker downloads count: 0.0%
- Dependent packages count: 0.006%
- Downloads: 0.009%
- Dependent repos count: 0.02%
- Average: 0.632%
- Forks count: 1.496%
- Stargazers count: 2.261%
- Maintainers (5)
- Advisories:
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 over 1 year ago)
- Last Synced: 2026-04-28T03:01:18.501Z (3 days ago)
- Versions: 82
- Dependent Packages: 599
- Dependent Repositories: 28,645
- Downloads: 70,223,276 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: v2.19.4+incompatible (published 12 days ago)
- Last Synced: 2026-04-26T20:01:52.685Z (4 days ago)
- Versions: 102
- 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 7 months ago)
- Last Synced: 2026-04-26T20:01:51.094Z (4 days ago)
- Versions: 3
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 1,074 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: 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 7 months ago)
- Last Synced: 2026-04-26T20:01:51.101Z (4 days ago)
- Versions: 3
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 1,074 Total
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 33.154%
- Downloads: 99.463%
- Maintainers (1)
Dependencies
- all_images ~> 0
- rake >= 0
- test-unit >= 0
- actions/checkout v2 composite
- ruby/setup-ruby v1 composite
- actions/checkout v6 composite
- actions/create-github-app-token v2 composite
- convictional/trigger-workflow-and-wait v1.6.5 composite
Score: 34.76163820066454