0

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

1
  • use SQLAlchemy to use ORM Commented Jun 6, 2020 at 12:45

1 Answer 1

1

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)

OUTPUT

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

2 Comments

Hi, thanks for the help, i already did it and have the error:TypeError: 'type' object is not subscriptable in the values = tuple code, have an idea?
Can you please put your code somewhere with error?, so that I can help you.

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.