How do I write the values of certain keys contained in the result.json file to the table I created. the values in the certain keys will be written in the columns I have created. I have id, title, score columns in my table. I want to write the value opposite the title key in the title column and the value opposite the score key in the score column.
result.son file:
[
{
"title": "Street Basketball Association",
"appId": "com.sba.android.googleplay",
"url": "https://play.google.com/store/apps/details?id=com.sba.android.googleplay",
"icon": "https://play-lh.googleusercontent.com/a17t8kK_A4TMJtV0l6oTGzOvLbu_QlnKiWZM3L4RB-xprNL57z1YAJlal9ewEPrWxrW3=w240-h480-rw",
"developer": "ShakaChen",
"currency": "USD",
"price": 0,
"free": true,
"summary": "We will bring you the hottest basketball game experience.You can invite other players to a real-time online competition or rank up while playing various league, cups and events in spectacular arenas.",
"scoreText": "4.0",
"score": 4.315321
}
]
the name of my database is test :in this database, I have a table with one test_table.
My code:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.dialects.postgresql import JSON
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://username:password@localhost:5432/test'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
class Test(test.Model):
__tablename__ = 'test_table'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255))
score = db.Column(db.Float)
with open('./result.json') as my_file:
dicts = load(my_file)
for dictionary in dicts:
test_data = Test(app_name=dictionary['title'],
developer=dictionary['developer'],
score=dictionary['score'])
db.session.add(test_data)
db.session.commit()