Ruby: Checking JSON and Naming Variables - ruby

I'm really new to working with JSON in Ruby, and am having a hard time figuring out why a script I'm running is generating blank lines as a result. Let's say that I'm parsing a file where one element is not always present. I want to check if that element exists, and depending on the result name variables in a specific way. Here is what I've been trying
require 'rubygems'
require 'json'
file = File.open("/path/to/file.json", encoding: 'UTF-8')
json = file.read
data = JSON.parse(json)
if data["snapshots"][-1]["responses"][2].nil?
item = something
elseif
item = something else
end
puts item
This is not generating an error, but is just producing a blank line. I'm sure I'm doing something obvious wrong, but would appreciate any help. Thanks!

Your main problem is that you had elseif instead of elsif
Full code using the JSON you provided below:
require 'json'
json = <<EOS
{
"snapshots": [
{
"steps": 10,
"responses": [
{
"tokens": [
"Answer"
],
"questionPrompt": "Question1?"
},
{
"tokens": [
"Answer"
],
"questionPrompt": "Question2?"
},
{
"locationResponse": {
"location": {
"speed": 0,
"timestamp": "2014-04-20T17: 28: 37-0400",
"longitude": "-xx.xxxxxxx",
"latitude": "xx.xxxxxx",
"verticalAccuracy": 3,
"course": 0,
"horizontalAccuracy": 5
},
"text": "Response"
},
"questionPrompt": "Question3?"
},
{
"tokens": [
"Answer"
],
"questionPrompt": "Question4?"
}
],
"battery": 0.75,
"sectionIdentifier": "1-2014-5-7",
"audio": {
"avg": -49.84988,
"peak": -39.73056
},
"background": 0,
"date": "2014-05-07T23: 20: 57-0400",
"location": {
"speed": -1,
"placemark": {
"subAdministrativeArea": "County",
"subLocality": "CityName",
"thoroughfare": "Street",
"administrativeArea": "xx",
"subThoroughfare": "xxx",
"postalCode": "xxxxx",
"region": "<+xx.xxxxxx",
"radius": 28.13,
"country": "UnitedStates",
"locality": "CityName",
"name": "Address"
},
"timestamp": "2014-05-07T23: 20: 58-0400",
"longitude": "-xx.xxxxxxx",
"latitude": "xx.xxxxxxx",
"verticalAccuracy": 10,
"course": 0,
"horizontalAccuracy": 65
},
"dwellStatus": 0,
"weather": {
"relativeHumidity": "68%",
"visibilityKM": 16.1,
"tempC": 13.3,
"precipTodayIn": 0,
"windKPH": 0,
"latitude": 40.813984,
"windDegrees": 159,
"stationID": "xxxxxxxx",
"visibilityMi": 10,
"pressureIn": 30.2,
"pressureMb": 1023,
"feelslikeF": 55.9,
"windGustKPH": 12.4,
"longitude": -77.895775,
"feelslikeC": 13.3,
"precipTodayMetric": 0,
"tempF": 55.9,
"windDirection": "SSE",
"dewpointC": 8,
"uv": 0,
"weather": "Overcast",
"windGustMPH": 7.7,
"windMPH": 0
},
"connection": 1,
"sync": 0,
"reportImpetus": 0,
"draft": 0
}
]
}
EOS
data = JSON.parse(json)
if data["snapshots"][-1]["responses"][2].nil?
item = "was nil"
elsif # THIS WAS elseif before
item = "NOT nil"
end
puts item

Related

Add new items to multidimensional Ruby hash in loop

Migrating my code to Ruby stuck on the updating this hash with new items inside the multidimensional hash>customers>image>
bannerhash = {
"stooge": "larry",
"toppings": [],
"customers": [
{
"id": 1,
"alt": "Image seo text",
"image": {
"label": "gid:///28663588552955",
"image": "https://cdn.doma.com/s/files/1/0611/2064/3323/files/yellow-pillow-bedside-table_2000x.jpg?v=1637223489"
},
"mainsize": 100
},
{
"id": 2,
"alt": "Image seo text",
"image": {
"label": "gid:///28663588487419",
"image": "https://cdn.doma.com/s/files/1/0611/2064/3323/files/bed-side-table_2000x.jpg?v=1637223489"
},
"mainsize": 100
},
{
"id": 3,
"alt": "image",
"themeposition": "388506648827/posA",
"image": {
"label": "gid:///28663588585723",
"image": "https://cdn.doma.com/s/files/1/0611/2064/3323/files/sunlight-creeps-through-a-bright-living-room_2000x.jpg?v=1637223489"
},
"mainsize": 87
},
{
"id": 4,
"alt": "short width",
"themeposition": "home",
"mainsize": 70,
"image": {
"label": "gid:///28663588454651",
"image": "https://cdn.doma.com/s/files/1/0611/2064/3323/files/modern-and-stylish-design_2000x.jpg?v=1637223489"
},
"quickcss": "#import url(\'https://fonts.googleapis.com/css2?family=Comic+Neue:wght#300&display=swap\');\n.wrapper-4 .--description {font-family: \'Comic Neue\', cursive; }"
}
]
}
Want to update in the loop and the add the "newitem" key with value inside the "image" like
{
"stooge": "larry",
"toppings": [],
"customers": [
{
"id": 1,
"alt": "Image seo text",
"image": {
"label": "gid:///28663588552955",
"image": "https://cdn.doma.com/s/files/1/0611/2064/3323/files/yellow-pillow-bedside-table_2000x.jpg?v=1637223489",
"newitem": "https://cdn.doma.com/s/files/1/0611/2064/3323/files/yellow-pillow-bedside-table_3300x.jpg?v=1637223489"
},
"mainsize": 100
},
{
"id": 2,
"alt": "Image seo text",
"image": {
"label": "gid:///28663588487419",
"image": "https://cdn.doma.com/s/files/1/0611/2064/3323/files/bed-side-table_2000x.jpg?v=1637223489",
"newitem": "https://cdn.doma.com/s/files/1/0611/2064/3323/files/bed-side-table_3300x.jpg?v=1637223489"
},
"mainsize": 100
}
]
}
My workaround code where I am stuck on how ruby update hash inside the each loop. First I catch the inner hash with the "customers" key and know the index and value however stuck on how to add "newitem" inside the "customers" "image" hash
if bannerhash.has_key?(:customers)
puts "found"
bannerhash.each_with_index do |(key, value), index|
if key == :customers
puts "index: #{index} | key: #{key} | value: #{value}"
# STACK Here bannerhash[key].each
end
end
else
puts "banners not found"
end
This seems pretty straightforward. No need to loop over all of the key/value pairs looking for :customers when we can access it directly.
if bannerhash.has_key?(:customers)
bannerhash[:customers].each { |h|
h[:image][:newitem] = "https://cdn.doma.com/s/files/1/0611/2064/3323/files/yellow-pillow-bedside- table_3300x.jpg?v=1637223489"
}
else
puts "banners not found"
end

Extracting selected data from a text file in Ruby

Now I am working on extracting information from a text file in Ruby.
Then how can I extract just the number '0.6748984055823062' from the following text file?
{
"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
},
What I have tried is that I could read a file and print it line by line by the following code.
counter = 1
file = File.new("Code.org", "r")
while (line = file.gets)
puts "#{counter}: #{line}"
counter = counter + 1
end
file.close
I want to set the number to a variable so that I can process it.
Here's a script which extracts just the score you want.
Two things to keep in mind :
the score you're looking for might not be the first one
the data is a mix of Arrays and Hashes
json_string = %q${
"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
}
]
}
]
}
$
require 'json'
json = JSON.parse(json_string)
puts json["sentiment_analysis"].first["positive"].first["score"]
#=> 0.6748984055823062
It looks like the data is a JSON string. In that case you can parse it and do something like the following:
require 'json'
file = File.read('Code.org')
data_hash = JSON.parse(file)
score = data_hash['score']

Transferring JSON Data into an array using ruby

This is my JSON code
{
"jobs": [
{
"id": 1,
"title": "Software Developer",
"applicants": [
{
"id": 1,
"name": "Rich Hickey",
"tags": ["clojure", "java", "immutability", "datomic", "transducers"]
},
{
"id": 2,
"name": "Guido van Rossum",
"tags": ["python", "google", "bdfl", "drop-box"]
}
]
},
{
"id": 2,
"title": "Software Architect",
"applicants": [
{
"id": 42,
"name": "Rob Pike",
"tags": ["plan-9", "TUPE", "go", "google", "sawzall"]
},
{
"id": 2,
"name": "Guido van Rossum",
"tags": ["python", "google", "bdfl", "drop-box"]
},
{
"id": 1337,
"name": "Jeffrey Dean",
"tags": ["spanner", "BigTable", "MapReduce", "deep learning", "massive clusters"]
}
]
}
]
}
I want to put the list of "Jobs" in an array using ruby.
I have the following code so far.
require 'json'
file = File.read(filepath)
data_hash = JSON.parse(file)
How do I iterate on the data_hash and chose what information I want and place it in an array?
You can use Array#each because data_hash['jobs'] contains an array of jobs:
data_hash['jobs'].each {|job| ... }
Like this,
arr = Array.new
data_hash.each { |job|
arr.insert(job['name'])
}
use Array#map for shorter code
data_hash['jobs'].map do |job|
# Do whatever you want with the job here
properties = %w(title applicants)
job.select{ |key| properties.include?(key) }
end

Please help me with my Ruby script (ruby 2.0.0p481)

I am writing a ruby script to accept an input file that is a CSV. I want to make a pretty JSON file. I feel that I am so close but I can't seem to get there. My original project was in JS but the requirements have changed to make it a Ruby file.
My input file looks like this
item id,description,price,cost,price_type,quantity_on_hand,size_1_name,size_1_price,size_2_name,size_2_price,size_3_name,size_3_price
one thing to note is that some of the values in the CSV file maybe missing because it doesn't exist.
require 'csv'
require 'json'
def is_int(str)
return !!(str =~ /^[-+]?[1-9]([0-9]*)?$/)
end
lines = CSV.open(ARGV[0],{:col_sep => ","}).readlines
# remove first entry of the lines array
keys = lines.shift
lines.each do |values|
# convert the line into a hash and transform string into int
#hash=Hash[keys.zip(values.map{|val| is_int(val) ? val.to_i : val}) ]
hash = keys.zip(values.map{ val})
# Write a file with the hash results
File.open("#{hash['NAME']}.json", "w") do |f|
f.write JSON.pretty_generate [hash]
end
end
The output I am trying to get is
[
{
id: 111010,
description: 'Coffee',
price: 1.25,
cost: 0.80,
price_type: 'system',
quantity_on_hand: 100000,
modifiers: [
{
name: 'Small',
price: -0.25
},{
name: 'Medium',
price: 0.00
},{
name: 'Large',
price: 0.30
}
]
the version of Ruby I'm using is 2.0.0p481
Error was
usr/lib/ruby/2.0.0/csv.rb:1254:in `initialize': no implicit conversion of nil into String (TypeError)
from /usr/lib/ruby/2.0.0/csv.rb:1254:in `open'
from /usr/lib/ruby/2.0.0/csv.rb:1254:in `open'
from stockimporter.rb:8:in `<main>'
csv.csv:
id,description,price,cost,price_type,quantity_on_hand,size_1_name,size_1_price,size_2_name,size_2_price,size_3_name,size_3_price
111010,Coffee,1.25,0.80,system,10000,Small,-0.25,Medium,0.00,Large,0.30
111011,Tea,1.00,0.50,system,100,Small,-0.10,Medium,0.00,Large,0.10
111012,MissingInfo,1.00,0.50,,100,,,Medium,,Large,0.10
...
require 'csv'
require 'pp'
require 'json'
csv_options = {
headers: true, #skip first line of csv file
converters: [:numeric] #convert strings that look like integers or floats to integers or floats
}
results = []
CSV.foreach('csv.csv', csv_options) do |row|
record = Hash[row.take(6)]
modifiers = [
{
name: row["size_1_name"],
price: row["size_1_price"]
},
{
name: row["size_2_name"],
price: row["size_2_price"],
},
{
name: row["size_3_name"],
price: row["size_3_price"]
}
]
record['modifiers'] = modifiers
results << record
end
pp results
--output:--
[{"id"=>111010,
"description"=>"Coffee",
"price"=>1.25,
"cost"=>0.8,
"price_type"=>"system",
"quantity_on_hand"=>10000,
"modifiers"=>
[{:name=>"Small", :price=>-0.25},
{:name=>"Medium", :price=>0.0},
{:name=>"Large", :price=>0.3}]},
{"id"=>111011,
"description"=>"Tea",
"price"=>1.0,
"cost"=>0.5,
"price_type"=>"system",
"quantity_on_hand"=>100,
"modifiers"=>
[{:name=>"Small", :price=>-0.1},
{:name=>"Medium", :price=>0.0},
{:name=>"Large", :price=>0.1}]},
{"id"=>111012,
"description"=>"MissingInfo",
"price"=>1.0,
"cost"=>0.5,
"price_type"=>nil,
"quantity_on_hand"=>100,
"modifiers"=>
[{:name=>nil, :price=>nil},
{:name=>"Medium", :price=>nil},
{:name=>"Large", :price=>0.1}]}]
json = JSON.pretty_generate(results)
puts json
--output:--
[
{
"id": 111010,
"description": "Coffee",
"price": 1.25,
"cost": 0.8,
"price_type": "system",
"quantity_on_hand": 10000,
"modifiers": [
{
"name": "Small",
"price": -0.25
},
{
"name": "Medium",
"price": 0.0
},
{
"name": "Large",
"price": 0.3
}
]
},
{
"id": 111011,
"description": "Tea",
"price": 1.0,
"cost": 0.5,
"price_type": "system",
"quantity_on_hand": 100,
"modifiers": [
{
"name": "Small",
"price": -0.1
},
{
"name": "Medium",
"price": 0.0
},
{
"name": "Large",
"price": 0.1
}
]
},
{
"id": 111012,
"description": "MissingInfo",
"price": 1.0,
"cost": 0.5,
"price_type": null,
"quantity_on_hand": 100,
"modifiers": [
{
"name": null,
"price": null
},
{
"name": "Medium",
"price": null
},
{
"name": "Large",
"price": 0.1
}
]
}
]
You could also do it like this:
CSV.foreach('csv.csv', csv_options) do |row|
record = Hash[row.take(6)]
price_adjustments = row.drop(6)
# [["size_1_name", "Small"], ["size_1_price", -0.25]]
# |---------------------------------------------------|
# ^
# |
modifiers = price_adjustments.each_slice(2).map do |size_price|
size_price.first[0] = 'name'
size_price.last[0] = 'price'
Hash[size_price]
end
p modifiers #e.g. [{"name"=>"Small", "price"=>-0.25}, {"name"=>"Medium", "price"=>0.0}, {"name"=>"Large", "price"=>0.3}]
record['modifiers'] = modifiers
results << record
end
pp results

Error Parsing JSON from YELP API

I am having trouble parsing JSON from Yelp API. The JSON data looks like this:
{
region: {
span: {
latitude_delta: 0,
longitude_delta: 0
},
center: {
latitude: 38.054117,
longitude: -84.439002
}
},
total: 23,
businesses: [
{
is_claimed: false,
rating: 5,
mobile_url: "http://m.yelp.com/biz/vineyard-community-church-lexington",
rating_img_url: "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png",
review_count: 2,
name: "Vineyard Community Church",
snippet_image_url: "http://s3-media4.ak.yelpcdn.com/photo/VoeMtbk7NRFi6diksSUtOQ/ms.jpg",
rating_img_url_small: "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png",
url: "http://www.yelp.com/biz/vineyard-community-church-lexington",
phone: "8592582300",
snippet_text: "I have been a member of Vineyard Community Church since 2004. Here you will find a modern worship service with a full band, witty speakers who teach...",
image_url: "http://s3-media3.ak.yelpcdn.com/bphoto/D71eikniuaHjdOC8DB6ziA/ms.jpg",
categories: [
[
"Churches",
"churches"
]
],
display_phone: "+1-859-258-2300",
rating_img_url_large: "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png",
id: "vineyard-community-church-lexington",
is_closed: false,
location: {
city: "Lexington",
display_address: [
"1881 Eastland Pwky",
"Lexington, KY 40505"
],
geo_accuracy: 8,
postal_code: "40505",
country_code: "US",
address: [
"1881 Eastland Pwky"
],
coordinate: {
latitude: 38.054117,
longitude: -84.439002
},
state_code: "KY"
}
}
]
}
The JSON is stored in a ruby string called #stuff
Here is the code I use to try and parse it:
#parsed_stuff = JSON::parse(#stuff)
When i do this and try and display the contents of # parsed_stuff, i get the following error in the browser
Parse error on line 2: { "region"=>{ "span -------------^ Expecting '}', ':', ',', ']'
Any help given on this issue will be highly appreciated.
Use jsonlint for validating JSON. Here you have to give all keys as a string.
Try it
{
"region": {
"span": {
"latitude_delta": 0,
"longitude_delta": 0
},
"center": {
"latitude": 38.054117,
"longitude": -84.439002
}
},
"total": 23,
"businesses": [
{
"is_claimed": false,
"rating": 5,
"mobile_url": "http://m.yelp.com/biz/vineyard-community-church-lexington",
"rating_img_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png",
"review_count": 2,
"name": "Vineyard Community Church",
"snippet_image_url": "http://s3-media4.ak.yelpcdn.com/photo/VoeMtbk7NRFi6diksSUtOQ/ms.jpg",
"rating_img_url_small": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png",
"url": "http://www.yelp.com/biz/vineyard-community-church-lexington",
"phone": "8592582300",
"snippet_text": "I have been a member of Vineyard Community Church since 2004. Here you will find a modern worship service with a full band, witty speakers who teach...",
"image_url": "http://s3-media3.ak.yelpcdn.com/bphoto/D71eikniuaHjdOC8DB6ziA/ms.jpg",
"categories": [
[
"Churches",
"churches"
]
],
"display_phone": "+1-859-258-2300",
"rating_img_url_large": "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png",
"id": "vineyard-community-church-lexington",
"is_closed": false,
"location": {
"city": "Lexington",
"display_address": [
"1881 Eastland Pwky",
"Lexington, KY 40505"
],
"geo_accuracy": 8,
"postal_code": "40505",
"country_code": "US",
"address": [
"1881 Eastland Pwky"
],
"coordinate": {
"latitude": 38.054117,
"longitude": -84.439002
},
"state_code": "KY"
}
}
]
}

Resources