0

I have a JSON File which looks like:

{"business_id": "vcNAWiLM4dR7D2nwwJ7nCA", "full_address": "4840 E Indian School Rd\nSte 101\nPhoenix, AZ 85018", "hours": {"Tuesday": {"close": "17:00", "open": "08:00"}, "Friday": {"close": "17:00", "open": "08:00"}, "Monday": {"close": "17:00", "open": "08:00"}, "Wednesday": {"close": "17:00", "open": "08:00"}, "Thursday": {"close": "17:00", "open": "08:00"}}, "open": true, "categories": ["Doctors", "Health & Medical"], "city": "Phoenix", "review_count": 7, "name": "Eric Goldberg, MD", "neighborhoods": [], "longitude": -111.98375799999999, "state": "AZ", "stars": 3.5, "latitude": 33.499313000000001, "attributes": {"By Appointment Only": true}, "type": "business"}
{"business_id": "JwUE5GmEO-sH1FuwJgKBlQ", "full_address": "6162 US Highway 51\nDe Forest, WI 53532", "hours": {}, "open": true, "categories": ["Restaurants"], "city": "De Forest", "review_count": 26, "name": "Pine Cone Restaurant", "neighborhoods": [], "longitude": -89.335843999999994, "state": "WI", "stars": 4.0, "latitude": 43.238892999999997, "attributes": {"Take-out": true, "Good For": {"dessert": false, "latenight": false, "lunch": true, "dinner": false, "breakfast": false, "brunch": false}, "Caters": false, "Noise Level": "average", "Takes Reservations": false, "Delivery": false, "Ambience": {"romantic": false, "intimate": false, "touristy": false, "hipster": false, "divey": false, "classy": false, "trendy": false, "upscale": false, "casual": false}, "Parking": {"garage": false, "street": false, "validated": false, "lot": true, "valet": false}, "Has TV": true, "Outdoor Seating": false, "Attire": "casual", "Alcohol": "none", "Waiter Service": true, "Accepts Credit Cards": true, "Good for Kids": true, "Good For Groups": true, "Price Range": 1}, "type": "business"}

I want to to traverse this file and convert individual structures into a Ruby hash so that I can access the different hashes to perform actions on them.

This is a hash of the first structure of the JSON file:

{"business_id"=>"uUsfpN81JCMKyH6c0D0bTg", "full_address"=>"1910 Village Center Cir\nSte 6\nSummerlin\nLas Vegas, NV 89134", "hours"=>{}, "open"=>true, "categories"=>["Food", "Desserts", "Italian", "Pizza", "Restaurants"], "city"=>"Las Vegas", "review_count"=>6, "name"=>"Rocco's NY Pizza & Pasta", "neighborhoods"=>["Summerlin"], "longitude"=>-115.3041999, }

Similarly I want to have hashes of all other JSON structures. How can I do this in Ruby on Rails?

1
  • Don't use a link to point to the data necessary to answer your question. When link-rot sets in your question will become nonsense because nobody will be able to tell what data you're talking about. Instead, supply a minimal sample of the data, as small as is necessary to reproduce the problem. Commented Nov 5, 2014 at 20:08

3 Answers 3

1

The file you got it's not a JSON file, but simply a text file with a bunch of JSON-encoded objects. Simply loop over the lines.

require 'json'

File.read("file.txt").split("\n").each do |line|
  JSON.parse(line)
end

or use .map instead of .each if you want return an Array of Hash.

File.foreach("file.txt").map do |line|
  JSON.parse(line)
end

File.read("file.txt").split("\n").map do |line|
  JSON.parse(line)
end

Also note this has nothing to do with Rails, it's pure Ruby code.

Sign up to request clarification or add additional context in comments.

3 Comments

Works like a charm, Thank you so much. Just one more question a json file would have comma separated structures or what as you said it's a text file? Cause I got this file from somewhere and it has around 25k records and has an extension json.
An array of items in JSON is defined as [{ ... }, { ... }].
File.foreach(...) is faster than File.read(...).split("\n").each.
1

Something like this will return an array of hashes retrieved from the file:

require 'json'

array_of_hashes = File.foreach('file.txt').map{ |l| JSON[l] }

Be warned though, this isn't scalable because you'll be pulling the entire file into memory, and if that resulting array is larger than the available space you'll see your program go to a crawl.

A more scalable approach would be to read each line, convert it to a Ruby object using JSON's parser, then immediately stuff the resulting object into a database record, and finally iterate over the database to do your massaging. SQLite3, PostgreSQL, MySQL would all be reasonable choices for this.

1 Comment

Ok I agree with what you are saying. Will keep in mind .Thanks :)
0

Have you tried JSON.parse

   require 'json'
   JSON.parse(your_json) #=> your expected result

1 Comment

Yes I did. Followed this article. It gives an error if there is more than 1 json structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.