1

I have a json file with a lot of movies in it. I want to create a model 'Movie' and fill it with all movies from that json file. How do i do it? I know that I can parse json file into a hash but that's not the thing I am looking for.

3 Answers 3

1

The correct term you're looking for is "seeding"!

You're going to need a database however, and a migration to create the database along with the associated movies table. (There are plenty of guides on how to do this, along with the official documentation).

After that's done, you'll need to "seed" your database with the data in your json file.

In the seeds.rb file, assuming that the JSON file is an array of Movies in JSON form, you should be able to loop over every Movie JSON object and insert it into your database.

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

1 Comment

Thank you! I've read the documentation about seeding and that is exactly what I have been looking for!
1

To add to docaholic's helpful response, here's some steps/pseudo-code that may help.

Assuming you're using a SQL database and need to create a model:

# creates a migration file. 
rails generate migration create_movies title:string #duration_in_minutes:integer or whatever fields you have
# edit the file to add other fields/ensure it has what you want.
rake db:migrate

Write a script to populate your database. There are many patterns for this (rake task, test fixtures, etc) and which one you'd want to use would depend on what you need (whether it's for testing, for production environment, as seed data for new environments, etc).

But generally what the code would look like is:

text_from_file = File.read(file_path)
JSON.parse(text_from_file).each do |json_movie_object|
  Movie.create!(title: json_movie_object[:title], other_attribute: json_movie_object[:other_attribute])
  # if the json attributes exactly match the column names, you can do
  # Movie.create!(json_movie_object)
end

This is not the most performant option for large amounts of data. For large files you can use insert_all for much greater efficiency, but this bypasses activerecord validations and callbacks so you'd want to understand what that means.

Comments

0

For my case, I need to seed around 200 hundred datas for production from a JSON file, so I tried to insert data from the json files in a database.

SO at first I created a database in rails project

rails generate migration create_movies title:string duration_in_minutes:integer
# or whatever fields you have

# edit the file to add other fields/ensure it has what you want.
rake db:migrate

Now its time to seed datas! Suppose your movies.json file has:

[
 {"id":"1", "name":"Titanic", "time":"120"},
 {"id":"2", "name":"Ruby tutorials", "time":"120"},,
 {"id":"3", "name":"Something spetial", "time":"500"}
 {"id":"4", "name":"2HAAS", "time":"320"}
]

NOW, like this 4 datas, think your JSON file has 400+ datas to input which is a nightmare to write in seed files manually. You need to add JSON gem to work. Run

bundle add json

to work with JSON file.

In db/seed.rb file, add these lines to add those 400+ infos in your DATABASE:

infos_from_json = File.read(your/JSON/file/path/movies.json)
JSON.parse(infos_from_json).each do |t|
  Movie.create!(title: t['name'], duration_in_minutes: 
t['time'])
end

Here:

  • infos_from_json variable fixing the JSON file from file directory

  • JSON.parse is calling the JSON datas then the variable is declared to indicate where the file is located and each do loop is dividing each data and |t| will be used to add part JSON.parse(infos_from_json). in every call.

  • Movie.create!(title: t['title'], duration_in_minutes: t['time']) is adding the data in database

This is how we can add datas in our database from a JSON file easily.

For more info about seeding data in database checkout the documentation

THANKS

Edit: This operation requires JSON gem just type bundle add json

Comments

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.