0

Good evening, I'm new here and I've encountered a problem working with python, flask, peewee, and PostgreSQL that I can't seem to figure out. I have already received data from an API and placed the data into my data.json file. Now I'm trying to take the data that is currently in the data.json file and transfer it into PostgreSQL. The assignment is to get data from an API, import it to PostgreSQL, and then perform create and read methods on the PostgreSQL table.

import json

with open('./data.json', 'w') as data:
 movies = json.dump(data)

This is how I've been approaching the problem, however, I haven't been successful in getting the data into PostgreSQL. Thank you!

5
  • You should try using Python SQLAlchemy lib for DB operations. Commented Jun 21, 2020 at 1:36
  • 1
    peewee is an ORM similar to SQLAlchemy, though lighter. It is not necessary to move off it. Commented Jun 21, 2020 at 2:50
  • @Unicone, ok, the assignment is for us to use PostgreSQL, not SQLAlchemy. Commented Jun 21, 2020 at 3:15
  • @AdrianKlaver, thanks for your input. You'll have to forgive my ignorance, I just started working with Python, Peewee, SQL, and Flask this week. I'm not sure I understand what you mean. I have to get the json data from the data.json file to the PostgreSQL database somehow, right? Commented Jun 21, 2020 at 3:18
  • @WRNobleJr Peewee or SQLAlchemy are ORMs (Object-relation mapping) it's an API that let you work easily with databases, in your case Peewee. You'd need to read the documentation of it to understand how it works and how you can use it to manage your database. Commented Jun 21, 2020 at 5:01

1 Answer 1

1

Since this is I assume a homework assignment I will point you in the direction. First the data is JSON and you want to get to Python structure so you need json.load(). What the output is will depend on what the JSON format is. The next thing is to determine whether the JSON contains objects(loaded to Python dicts) that have keys equal to the database table column names. Not sure if you have a Peewee model that maps to a Postgres table, but if you do and the keys/column names match then you can pass each JSON object/now(after load()) Python dict to the model and save(). A good place to start is:

http://docs.peewee-orm.com/en/latest/peewee/quickstart.html

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

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.