JSON and related Flashcards

1
Q

Import json

A

import json

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you convert from json to python?

A

import json
x = ‘{“name”:”John”, “age”:30, “city”:”New York”}’

y = json.loads(x)

print(y[‘age’])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you convert from python to json?

A

import json

x = {
“name”: “John”,
“age”: 30,
“city”: “New York”
}

y = json.dumps(x)

print(y)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the below? How would you convert this?

x = {
“this”: “that”,
“the”: “other”
}

A

That is python, so you would convert to json

Jason would eat the python and take a dump. The byproduct is json

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the below? How would you convert it?
x = { “this”: “that”, “the”: “other” }

A

JSON so you would convert it to python

The magician loads Jason into a magic box, he shuts the door and twirls his wand. Upon opening the door we see that Jason has changed into a python

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

which conversion type can we change things around on?

A

Remember, JSON works with json data so it manipulates JSON, not python.

Jason eats the python and takes a messy dump. We have to pick out the corn now!
Havery DENT comes in and claims he can fix this.
He pulls out a short knubby pair of keys, and then SiPs coffee from A florida gATORS mug before getting to work
json.dumps(x, indent=4, separators=(“.”,”=”), sort_keys=True)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What would you use to ask for the html info from a webpage

A

import requests

You go on a QUEST to find the page
pip install requests

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How would you get the html page from:
https://w3schools.com/python/demopage.html?

A

import requests
x = requests.get(‘https://w3schools.com/python/demopage.html’)

print(x.text)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What HTTP requests can you do?

A

delete - delete a resource
get - data retrieval
head - header info only
patch - Like put but for partial mods
post - submit data
put - replace resource
request - sends a request of a specific method to the specified url

DELsin runs at super speeds to GET a banana from a farmer’s market.
He then takes off going 90 miles an hour.
Along the way he runs past a POSTed speed limit of 50 mph and laughs at it.
He arrives at a farmers market in Japan and PUTs the banana in one of the carts before taking off running again.
Unfortunately he wasn’t paying attenting and ran right into a samurai’s sword who was training cutting his HEAD off.
Guts finds his head and vows to go on QUEST to get it place back on.
He finds a wizard to help. The wizard casts a long mystical spell and poof! Delsin’s head is PATHed back on with a bandaid.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What sites can we use to test requests and posts?

A

postb.in
requestbin.com

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How would you verify that you’re receiving a success code?

A

url = “you_url”
data = {“json”:”data”}
response = requests.post(url, json=data)

if response.status_code == 200:
print(“success”)
else:
print(“failure”)

or
response = requests.get(url)
if response.status_code == 200:
print(response.text)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you get info from an api with requests?

A

import requests

url = “https://url”

response = requests.get(url)

if response.status_code == 200:
data = response.json()
print(data)
else:
print(“error”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Install mysql as well as pandas

A

pip install mysql-connector-python

pip install pandas

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

import everything you need to run mysql

A

import mysql.connector
from mysql.connector import Error
import pandas as pd

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Download MYSQL community on your computer

A

https://www.mysql.com/downloads/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Create a connection to a database

A

import mysql.connector
import pymysql <- do this instead
mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”
)

print(mydb)

Remember to install cryptography

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Createa database named: mydatabase

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”
)

mycursor = mydb.cursor()

mycursor.execute(“CREATE DATABASE mydatabase”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Check if your database exists

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”
)

mycursor = mydb.cursor()

mycursor.execute(“SHOW DATABASES”)

for x in mycursor:
print(x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

If your database exists you can just connect, do this

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

20
Q

Create a table called customers

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

mycursor.execute(“CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))”)

21
Q

confirm the table exists

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

mycursor.execute(“SHOW TABLES”)

for x in mycursor:
print(x)

22
Q

What is a primary key?

Create one

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

mycursor.execute(“CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))”)

The business dolphin throws his ID INTO the automobile and the automobile start to duplicate (auto_increment)
The business dolphin calls it a day and goes to O’Charlies to get the PRIME RIB for dinner, he cuts it up with his keys

23
Q

When creating a table, you should also create a column with a unique key for each record.

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

mycursor.execute(“CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))”)

24
Q

Create a primary key for an existing table

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

mycursor.execute(“ALTER TABLE customers ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY”)

25
Q

Insert a record into the customers table

save it and show the number of rows affected by the last execute command

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

sql = “INSERT INTO customers (name, address) VALUES (%s, %s)”
val = (“John”, “Highway 21”)
mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, “record inserted.”)

mydb.commit() actually applies changes

26
Q

Insert multiple rows of customers into a table

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

sql = “INSERT INTO customers (name, address) VALUES (%s, %s)”
val = [
(‘Peter’, ‘Lowstreet 4’),
(‘Amy’, ‘Apple st 652’),
(‘Hannah’, ‘Mountain 21’),
(‘Michael’, ‘Valley 345’),
(‘Sandy’, ‘Ocean blvd 2’),
(‘Betty’, ‘Green Grass 1’),
(‘Richard’, ‘Sky st 331’),
(‘Susan’, ‘One way 98’),
(‘Vicky’, ‘Yellow Garden 2’),
(‘Ben’, ‘Park Lane 38’),
(‘William’, ‘Central st 954’),
(‘Chuck’, ‘Main Road 989’),
(‘Viola’, ‘Sideway 1633’)
]

mycursor.executemany(sql, val)

mydb.commit()

print(mycursor.rowcount, “was inserted.”)

27
Q

Ask the cursor to show you the last inserted row

Remember, you have to actually insert something for it to work

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

sql = “INSERT INTO customers (name, address) VALUES (%s, %s)”
val = (“Michelle”, “Blue Village”)
mycursor.executemany(sql, val)

mydb.commit()

print(“1 record inserted, ID:”, mycursor.lastrowid)

28
Q

Get all records from the customer table and print them

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

mycursor.execute(“SELECT * FROM customers”)

myresult = mycursor.fetchall()

for x in myresult:
print(x)

fetchall() gets all rows from the last executed statement

29
Q

print only the names, addresses columns from customers tables

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

mycursor.execute(“SELECT name, address FROM customers”)

myresult = mycursor.fetchall()

for x in myresult:
print(x)

30
Q

Only get the first row from the db

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

mycursor.execute(“SELECT * FROM customers”)

myresult = mycursor.fetchone()

print(myresult)

31
Q

Select record’s that contain the address Park Lane 38

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

sql = “SELECT * FROM customers WHERE address =’Park Lane 38’”

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
print(x)

32
Q

Get records where the address contains the word “way”

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

sql = “SELECT * FROM customers WHERE address LIKE ‘%way%’”

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
print(x)

33
Q

What is used to escape values?
Why is this needed?

A

%s
This is needed to prevent sql injections from users

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

sql = “SELECT * FROM customers WHERE address = %s”
adr = (“Yellow Garden 2”, )

mycursor.execute(sql, adr)

myresult = mycursor.fetchall()

for x in myresult:
print(x)

34
Q

Get everything from you table and sort it by name in ascending order

How do you sort by descending order

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

sql = “SELECT * FROM customers ORDER BY name”

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
print(x)

sql = “SELECT * FROM customers ORDER BY name DESC”

35
Q

Delete records with the address Mountain 21

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

sql = “DELETE FROM customers WHERE address = ‘Mountain 21’”

mycursor.execute(sql)

mydb.commit()

print(mycursor.rowcount, “record(s) deleted”)

IF YOU OMIT WHERE IT WILL DELETE ALL RECORDS

36
Q

Use an escape value to delete Yellow Garden 2 addresses

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

sql = “DELETE FROM customers WHERE address = %s”
adr = (“Yellow Garden 2”, )

mycursor.execute(sql, adr)

mydb.commit()

print(mycursor.rowcount, “record(s) deleted”)

37
Q

Delete an entire table

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

sql = “DROP TABLE customers”

mycursor.execute(sql)

38
Q

Avoid getting an error for trying to delete a table that doesn’t exist

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

sql = “DROP TABLE IF EXISTS customers”

mycursor.execute(sql)

39
Q

Overwrite the address column from ‘Canyon 123’ to ‘Valley 345’

How would you do this with an escape character?

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

sql = “UPDATE customers SET address = ‘Canyon 123’ WHERE address = ‘Valley 345’”

mycursor.execute(sql)

mydb.commit()

print(mycursor.rowcount, “record(s) affected”)

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

sql = “UPDATE customers SET address = %s WHERE address = %s”
val = (“Valley 345”, “Canyon 123”)

mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, “record(s) affected”)

40
Q

Grab all characters but make sure you only get the first 5 records

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

mycursor.execute(“SELECT * FROM customers LIMIT 5”)

myresult = mycursor.fetchall()

for x in myresult:
print(x)

41
Q

Grabe 5 records starting at the third record

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

mycursor.execute(“SELECT * FROM customers LIMIT 5 OFFSET 2”)

myresult = mycursor.fetchall()

for x in myresult:
print(x)

42
Q

You have two tables
One contains users names and a number that correlates to another table that contains favorite icecreams by an id number.

How do you combine fav and id?

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

sql = “SELECT \
users.name AS user, \
products.name AS favorite \
FROM users \
INNER JOIN products ON users.fav = products.id”

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
print(x)

43
Q

What is the difference between INNER JOIN and LEFT JOIN and RIGHT JOIN?

A

INNER JOIN only shows the records where there is a match.

If you want to show all users, even if they do not have a favorite product, use the LEFT JOIN statement:

RIGHT JOIN If you want to return all products, and the users who have them as their favorite, even if no user have them as their favorite, use the RIGHT JOIN statement

44
Q

Select all users and their favorite product:

A

sql = “SELECT \
users.name AS user, \
products.name AS favorite \
FROM users \
LEFT JOIN products ON users.fav = products.id”

45
Q

What are html headers?

A

Part of communication with server. These provide metadata

headers appear in the communication,
for instance when you make a request to a server one of the headers are User-Agent

46
Q

How do you change headers with request to get api info?

A

response = requests.get(url, headers={“Authorization”: f”Bearer {api_key”})