patternrubyCritical
Parsing a JSON string in Ruby
Viewed 0 times
stringparsingjsonruby
Problem
I have a string that I want to parse in Ruby:
Is there an easy way to extract the data?
string = '{"desc":{"someKey":"someValue","anotherKey":"value"},"main_item":{"stats":{"a":8,"b":12,"c":10}}}'Is there an easy way to extract the data?
Solution
This looks like JavaScript Object Notation (JSON). You can parse JSON that resides in some variable, e.g.
If you’re using an older Ruby, you may need to install the json gem.
There are also other implementations of JSON for Ruby that may fit some use-cases better:
json_string, like so:require 'json'
JSON.parse(json_string)If you’re using an older Ruby, you may need to install the json gem.
There are also other implementations of JSON for Ruby that may fit some use-cases better:
- YAJL C Bindings for Ruby
- JSON::Stream
Code Snippets
require 'json'
JSON.parse(json_string)Context
Stack Overflow Q#5410682, score: 647
Revisions (0)
No revisions yet.