Parsing a JSON string in Ruby
This looks like JavaScript Object Notation (JSON). You can parse JSON that resides in some variable, e.g. 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
Parse a json that contain json string
Just call JSON.parse
on the values again:
obj.transform_values { |v| JSON.parse(v) }
#=> {:Name=>{"FirstName"=>"Douglas", "LastName"=>"Crockford"}}
When you are still on Ruby <2.4 then there a more steps:
obj.map { |k, v| [k, JSON.parse(v)] }.to_h
Parsing from a JSON file in Ruby and Extract numbers from Nested Hashes
You can use Array#map
to collect the reviews.
reviews = json['sentiment_analysis'][0]
positive_reviews = reviews['positive']
negative_reviews = reviews['negative']
positive_reviews.map { |review| review['score'] }
=> [0.6748984055823062, 0.6280145725181376]
negative_reviews.map { |review| review['score'] }
=> [-0.7923352042939829, -0.5734506634410159]
Hope this helps!
How to parsing JSON string parameters in rails
You could do something like this with native Ruby JSON library:
@json = JSON.parse(request.body.read)
username = @json['username']
password = @json['password']
You take the POST request data and then parse to JSON.
Parsing JSON in ruby?
JSON.parse() expects a string input. So, array can't be used here.
Instead you can try as follows,
JSON.parse('[{"test": "test_a", "doc_type": { "id": 32 }}]')
Or
JSON.parse(arrayResponse.to_json)
to_json returns JSON string representation. Doc: https://apidock.com/rails/Hash/to_json
Ruby/Rails convert string to JSON - JSON.parse produces JSON::ParserError
It's an issue on versions of json gem (<2). To avoid it you should use versions of json >2. So you might try to upgrade your rails version or just use quirks_mode: true
.
JSON.parse(json_test, quirks_mode: true)
Here is a link to the issue on github json issue
Parsing JSON data from Ruby variable
You can't toss the @result data directly to JS because, although what you think, it is not valid JSON (noted the hash-rockets “=>”?)
The solution is to properly format the @result data as JSON, using ruby, then passing the resulting data to JS. For that you can simply do:
require 'json'
@result.to_json
And then, on JS side:
var apiData = JSON.parse('<%= @result %>');
Related Topics
Restarting a Loop from the Top
How to Pass Command Line Arguments to a Rake Task
Is 'Eval' Supposed to Be Nasty
How to Match All Occurrences of a Regex
Disable Activerecord For Rails 4
Altering the Primary Key in Rails to Be a String
Best Way to Convert Strings to Symbols in Hash
How to Run a Rake Task from Capistrano
Rails Update_Attributes Without Save
Find Out If Current Time Is Between Two Times
Installed Ruby 1.9.3 With Rvm But Command Line Doesn't Show Ruby -V
Difference Between Include and Extend in Ruby
How to Modify Path For Homebrew
Limitations in Running Ruby/Rails on Windows
Ruby: Inherit Code That Works With Class Variables