简体   繁体   中英

Read json file to create database in postgres using python

Hi im creating a DB using Python and want to extract the info from this json dict i dont know if it better to extract the info directly from the file:

data = 
{
"b_sales": [{
    "position_r": 1,
    "position": 1,
    "title": "Noda Dorado",
    "sales": "A088R7S66W",
    "link": "https://www.url.com/nonda-Adapter"
    "image": "https://image.jpg",
    "price_lower": {
        "value": 7.99,
    },
    "price_upper": {
        "value": 10.99,
    },
    "price": {
        "value": 7.99,
    }
}, {
    "position_r": 2,
    "title": "WHOOSH! Kit de limpieza de pantalla",
    "sales": "A07ZMFQB2S",
    "link": "https://www.url.com/WHOOSH-Screen-",
    "image": "https://images.jpg",
    "price_lower": {
        "value": 15.49,
    },
    "price_upper": {
        "value": 29.99,
    },
    "price": {
        "value": 15.49,
    }
}],
"pagination": {
    "current_page": 1,
    "total_pages": 2
}
}

i already started with the code:

import psycopg2

conn = psycopg2.connect(
database="postgres", user='postgres', password='password', host='127.0.0.1', port= '5432'
)
conn.autocommit = True

cursor = conn.cursor()

sql ='''CREATE TABLE b_sales (
TITLE CHAR(30) NOT NULL,
SALES CHAR(20),
LINK CHAR(40),
IMAGE CHAR(30),
PRICE_L INT,
PRICE_U INT
PRICE INT
)'''
cursor.execute(sql)
conn.close()

And i dont know how to add the info in the table quite good i tried but without success

You can try this solution to insert you data into database

import psycopg2

connection = conn = psycopg2.connect(
  database="postgres", user='postgres', password='password', 
  host='127.0.0.1', port= '5432'
)
connection.autocommit = True

cursor = connection.cursor()

sql ='''CREATE TABLE b_sales (
   TITLE CHAR(30) NOT NULL,
   SALES CHAR(20),
   LINK CHAR(40),
   IMAGE CHAR(30),
   PRICE_L INT,
   PRICE_U INT,
   PRICE INT
)'''
cursor.execute(sql)
connection.close()


def insert_data_into_db(TITLE,SALES, LINK, IMAGE, PRICE_L, PRICE_U, 
PRICE):
   import psycopg2
   connection = psycopg2.connect(
      database="postgres", user='postgres', password='password', 
      host='127.0.0.1', port= '5432' 
   )
   try:
      cursor = connection.cursor()
      cursor.execute(
        "INSERT INTO b_sales(TITLE, SALES, LINK, IMAGE, PRICE_L, 
         PRICE_U, PRICE) VALUES(%s, %s, %s, %s, %s, %s, %s)", 
         (TITLE,SALES, LINK, IMAGE, PRICE_L, PRICE_U, PRICE)
      )
      connection.commit()
   except Exception as e:
      print (e.message)
   finally:
      connection.close()

for item in data.get("b_sales"): # your python json data
      TITLE = item.get("title", "")
      SALES = item.get("sales", "")
      LINK = item.get("link", "")
      IMAGE = item.get("image", "")
      PRICE_L = item.get("price_lower", {}).get("value", 0)
      PRICE_U = item.get("price_upper", {}).get("value", 0)
      PRICE = item.get("price", {}).get("value", 0)
      # Inserting data into db
      insert_data_into_db(TITLE, SALES, LINK, IMAGE, PRICE_L, 
           PRICE_U, PRICE)

输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM