Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing from a JSON file in Ruby and Extract numbers from Nested Hashes

Tags:

json

parsing

ruby

Now I am working on extracting information from a JSON file in Ruby. Then how can I extract just the numbers next to the word 'score' from the following text file? For example, I want to get 0.6748984055823062, 0.6280145725181376 on and on.

{   "sentiment_analysis": [     {       "positive": [         {           "sentiment": "Popular",           "topic": "games",           "score": 0.6748984055823062,           "original_text": "Popular games",           "original_length": 13,           "normalized_text": "Popular games",           "normalized_length": 13,           "offset": 0         },         {           "sentiment": "engaging",           "topic": "pop culture-inspired games",           "score": 0.6280145725181376,           "original_text": "engaging pop culture-inspired games",           "original_length": 35,           "normalized_text": "engaging pop culture-inspired games",           "normalized_length": 35,           "offset": 370         },      "negative": [     {       "sentiment": "get sucked into",       "topic": "the idea of planning",       "score": -0.7923352042939829,       "original_text": "Students get sucked into the idea of planning",       "original_length": 45,       "normalized_text": "Students get sucked into the idea of planning",       "normalized_length": 45,       "offset": 342     },     {       "sentiment": "be daunted",       "topic": null,       "score": -0.5734506634410159,       "original_text": "initially be daunted",       "original_length": 20,       "normalized_text": "initially be daunted",       "normalized_length": 20,       "offset": 2104     }, 

What I have tried is that I could read a file and set the text file to a hash variable using the JSON method.

require 'json' json = JSON.parse(json_string) 
like image 707
Sookie J Avatar asked Dec 02 '16 23:12

Sookie J


People also ask

What is JSON parse () method?

parse() The JSON. parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

What does JSON parse return in Ruby?

If you ever got annoyed by the fact that JSON. parse returns hash with string keys and prefer hashes with symbols as keys, this post is for you.

Is JSON parse safe Ruby?

There are no security concerns possible. JSON isn't code, you can't inject harmful values into it. JSON. parse is safe.


1 Answers

Using the JSON class:

Importing a file:

require "json" file = File.open "/path/to/your/file.json" data = JSON.load file 

Optionally, you can close it now:

file.close 

The file looks like this:

{   "title": "Facebook",   "url": "https://www.facebook.com",   "posts": [     "lemon-car",     "dead-memes"   ] } 

The file is now able to be read like this:

data["title"] => "Facebook" data.keys => ["title", "url", "posts"] data['posts'] => ["lemon-car", "dead-memes"] data["url"] => "https://www.facebook.com" 

Hope this helped!

like image 125
Michal Štein Avatar answered Oct 10 '22 17:10

Michal Štein