Fetch, parse, and store JSON in PostgreSQL

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite
1












I was given a task to read JSON from a URL and then parse it in python. Each field will be assigned to a variable to store the value. The value will then be stored into postgres table.



Example of JSON




"forecasts":
[

"class": "fod_long_range_daily",
"expire_time_gmt": 1525126617,
"night":
"fcst_valid": 1525158000,
"fcst_valid_local": "2018-05-01T19:00:00+1200",
"golf_category": ""
,
"day":
"uv_warning": 0,
"uv_desc": "Moderate",
"golf_index": 10,
"golf_category": "Excellent"


]



I was told that this way i am able to parse JSON and read it into postgres. Is this the right way to do this? Will there be performance issues?



import urllib3
import psycopg2
import json

conn = psycopg2.connect(host="localhost", database="nzaus",
user="admin", password="123321")
print("Database Connected")
cur = conn.cursor()
rowcount = cur.rowcount

http = urllib3.PoolManager()
url = "https://api.data.com/v1/geocode/-35.228208/174.095969/forecast/daily/15day.json?language=en-US&units=m&apiKey=1234"
try:
response = http.request('GET', url)
data = json.loads(response.data.decode('utf-8'))

for item in data['forecasts']:
class = None
time = None
fcst_valid = None
golf_category = None

result =

class = item['class']
time = item['expire_time_gmt']
fcst_valid = item['night']['fcst_valid']
golf_category = item['morning']['golf_category']
result = [class,time,fcst_valid,golf_category]

cur.execute("""INSERT into datatable
VALUES
( %s,
%s,
%s,
%s,
)""",(result))
conn.commit()
cur.close()
except IOError as io:
print("cannot open")






share|improve this question





















  • version 3, using urllib3
    – Derek
    May 1 at 23:33
















up vote
3
down vote

favorite
1












I was given a task to read JSON from a URL and then parse it in python. Each field will be assigned to a variable to store the value. The value will then be stored into postgres table.



Example of JSON




"forecasts":
[

"class": "fod_long_range_daily",
"expire_time_gmt": 1525126617,
"night":
"fcst_valid": 1525158000,
"fcst_valid_local": "2018-05-01T19:00:00+1200",
"golf_category": ""
,
"day":
"uv_warning": 0,
"uv_desc": "Moderate",
"golf_index": 10,
"golf_category": "Excellent"


]



I was told that this way i am able to parse JSON and read it into postgres. Is this the right way to do this? Will there be performance issues?



import urllib3
import psycopg2
import json

conn = psycopg2.connect(host="localhost", database="nzaus",
user="admin", password="123321")
print("Database Connected")
cur = conn.cursor()
rowcount = cur.rowcount

http = urllib3.PoolManager()
url = "https://api.data.com/v1/geocode/-35.228208/174.095969/forecast/daily/15day.json?language=en-US&units=m&apiKey=1234"
try:
response = http.request('GET', url)
data = json.loads(response.data.decode('utf-8'))

for item in data['forecasts']:
class = None
time = None
fcst_valid = None
golf_category = None

result =

class = item['class']
time = item['expire_time_gmt']
fcst_valid = item['night']['fcst_valid']
golf_category = item['morning']['golf_category']
result = [class,time,fcst_valid,golf_category]

cur.execute("""INSERT into datatable
VALUES
( %s,
%s,
%s,
%s,
)""",(result))
conn.commit()
cur.close()
except IOError as io:
print("cannot open")






share|improve this question





















  • version 3, using urllib3
    – Derek
    May 1 at 23:33












up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





I was given a task to read JSON from a URL and then parse it in python. Each field will be assigned to a variable to store the value. The value will then be stored into postgres table.



Example of JSON




"forecasts":
[

"class": "fod_long_range_daily",
"expire_time_gmt": 1525126617,
"night":
"fcst_valid": 1525158000,
"fcst_valid_local": "2018-05-01T19:00:00+1200",
"golf_category": ""
,
"day":
"uv_warning": 0,
"uv_desc": "Moderate",
"golf_index": 10,
"golf_category": "Excellent"


]



I was told that this way i am able to parse JSON and read it into postgres. Is this the right way to do this? Will there be performance issues?



import urllib3
import psycopg2
import json

conn = psycopg2.connect(host="localhost", database="nzaus",
user="admin", password="123321")
print("Database Connected")
cur = conn.cursor()
rowcount = cur.rowcount

http = urllib3.PoolManager()
url = "https://api.data.com/v1/geocode/-35.228208/174.095969/forecast/daily/15day.json?language=en-US&units=m&apiKey=1234"
try:
response = http.request('GET', url)
data = json.loads(response.data.decode('utf-8'))

for item in data['forecasts']:
class = None
time = None
fcst_valid = None
golf_category = None

result =

class = item['class']
time = item['expire_time_gmt']
fcst_valid = item['night']['fcst_valid']
golf_category = item['morning']['golf_category']
result = [class,time,fcst_valid,golf_category]

cur.execute("""INSERT into datatable
VALUES
( %s,
%s,
%s,
%s,
)""",(result))
conn.commit()
cur.close()
except IOError as io:
print("cannot open")






share|improve this question













I was given a task to read JSON from a URL and then parse it in python. Each field will be assigned to a variable to store the value. The value will then be stored into postgres table.



Example of JSON




"forecasts":
[

"class": "fod_long_range_daily",
"expire_time_gmt": 1525126617,
"night":
"fcst_valid": 1525158000,
"fcst_valid_local": "2018-05-01T19:00:00+1200",
"golf_category": ""
,
"day":
"uv_warning": 0,
"uv_desc": "Moderate",
"golf_index": 10,
"golf_category": "Excellent"


]



I was told that this way i am able to parse JSON and read it into postgres. Is this the right way to do this? Will there be performance issues?



import urllib3
import psycopg2
import json

conn = psycopg2.connect(host="localhost", database="nzaus",
user="admin", password="123321")
print("Database Connected")
cur = conn.cursor()
rowcount = cur.rowcount

http = urllib3.PoolManager()
url = "https://api.data.com/v1/geocode/-35.228208/174.095969/forecast/daily/15day.json?language=en-US&units=m&apiKey=1234"
try:
response = http.request('GET', url)
data = json.loads(response.data.decode('utf-8'))

for item in data['forecasts']:
class = None
time = None
fcst_valid = None
golf_category = None

result =

class = item['class']
time = item['expire_time_gmt']
fcst_valid = item['night']['fcst_valid']
golf_category = item['morning']['golf_category']
result = [class,time,fcst_valid,golf_category]

cur.execute("""INSERT into datatable
VALUES
( %s,
%s,
%s,
%s,
)""",(result))
conn.commit()
cur.close()
except IOError as io:
print("cannot open")








share|improve this question












share|improve this question




share|improve this question








edited May 1 at 15:03









Sam Onela

5,77461543




5,77461543









asked May 1 at 9:52









Derek

212




212











  • version 3, using urllib3
    – Derek
    May 1 at 23:33
















  • version 3, using urllib3
    – Derek
    May 1 at 23:33















version 3, using urllib3
– Derek
May 1 at 23:33




version 3, using urllib3
– Derek
May 1 at 23:33










1 Answer
1






active

oldest

votes

















up vote
1
down vote













I would narrow down your try:except to only the statement(s) which could throw the error. Having such a large chunk of code in a try:except block could result in many exceptions thrown, none of which would be caught as you're only looking for IOError.



Also, for performance, you should build the statement and the data set from the loop, then throw the statement against cursor.executemany(statement, data) as a single database call instead.



Regarding construction, your code is just one huge blob. It's important that you refactor each operation into its own function, this saves on tracking down errors, and if you make changes in the future, your changes only affect one function. If the change is not liked by the rest of your code, it's easy to back out.

Can you imagine if you made 5 or 10 changes in your program as it is right now, and then it stops working? Which change was the breaking change? How would you find out? Why did it break? etc. Also, I'm not talking just simple syntax errors, I'm also talking about logic errors or data errors. These can be hard to track down if you don't have any tests validating your code.



Good luck!






share|improve this answer





















  • Thanks for the advice ! Does that mean that I should store the json results into an array/list, then when insert into postgres just read from the array/list?
    – Derek
    May 4 at 9:15










  • Perhaps a tuple added to a list? e.g.: [(class, time_, fcst_valid, category), (class, time_, fcst_valid, category), etc]
    – C. Harley
    May 5 at 11:55










  • So if i use psycopg2, just cur.execute(""" insert into table A( class, time_, fcst_valid, category) VALUES ( %s,%s,%s,%s)""",(list)) ?
    – Derek
    May 6 at 20:03










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193341%2ffetch-parse-and-store-json-in-postgresql%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













I would narrow down your try:except to only the statement(s) which could throw the error. Having such a large chunk of code in a try:except block could result in many exceptions thrown, none of which would be caught as you're only looking for IOError.



Also, for performance, you should build the statement and the data set from the loop, then throw the statement against cursor.executemany(statement, data) as a single database call instead.



Regarding construction, your code is just one huge blob. It's important that you refactor each operation into its own function, this saves on tracking down errors, and if you make changes in the future, your changes only affect one function. If the change is not liked by the rest of your code, it's easy to back out.

Can you imagine if you made 5 or 10 changes in your program as it is right now, and then it stops working? Which change was the breaking change? How would you find out? Why did it break? etc. Also, I'm not talking just simple syntax errors, I'm also talking about logic errors or data errors. These can be hard to track down if you don't have any tests validating your code.



Good luck!






share|improve this answer





















  • Thanks for the advice ! Does that mean that I should store the json results into an array/list, then when insert into postgres just read from the array/list?
    – Derek
    May 4 at 9:15










  • Perhaps a tuple added to a list? e.g.: [(class, time_, fcst_valid, category), (class, time_, fcst_valid, category), etc]
    – C. Harley
    May 5 at 11:55










  • So if i use psycopg2, just cur.execute(""" insert into table A( class, time_, fcst_valid, category) VALUES ( %s,%s,%s,%s)""",(list)) ?
    – Derek
    May 6 at 20:03














up vote
1
down vote













I would narrow down your try:except to only the statement(s) which could throw the error. Having such a large chunk of code in a try:except block could result in many exceptions thrown, none of which would be caught as you're only looking for IOError.



Also, for performance, you should build the statement and the data set from the loop, then throw the statement against cursor.executemany(statement, data) as a single database call instead.



Regarding construction, your code is just one huge blob. It's important that you refactor each operation into its own function, this saves on tracking down errors, and if you make changes in the future, your changes only affect one function. If the change is not liked by the rest of your code, it's easy to back out.

Can you imagine if you made 5 or 10 changes in your program as it is right now, and then it stops working? Which change was the breaking change? How would you find out? Why did it break? etc. Also, I'm not talking just simple syntax errors, I'm also talking about logic errors or data errors. These can be hard to track down if you don't have any tests validating your code.



Good luck!






share|improve this answer





















  • Thanks for the advice ! Does that mean that I should store the json results into an array/list, then when insert into postgres just read from the array/list?
    – Derek
    May 4 at 9:15










  • Perhaps a tuple added to a list? e.g.: [(class, time_, fcst_valid, category), (class, time_, fcst_valid, category), etc]
    – C. Harley
    May 5 at 11:55










  • So if i use psycopg2, just cur.execute(""" insert into table A( class, time_, fcst_valid, category) VALUES ( %s,%s,%s,%s)""",(list)) ?
    – Derek
    May 6 at 20:03












up vote
1
down vote










up vote
1
down vote









I would narrow down your try:except to only the statement(s) which could throw the error. Having such a large chunk of code in a try:except block could result in many exceptions thrown, none of which would be caught as you're only looking for IOError.



Also, for performance, you should build the statement and the data set from the loop, then throw the statement against cursor.executemany(statement, data) as a single database call instead.



Regarding construction, your code is just one huge blob. It's important that you refactor each operation into its own function, this saves on tracking down errors, and if you make changes in the future, your changes only affect one function. If the change is not liked by the rest of your code, it's easy to back out.

Can you imagine if you made 5 or 10 changes in your program as it is right now, and then it stops working? Which change was the breaking change? How would you find out? Why did it break? etc. Also, I'm not talking just simple syntax errors, I'm also talking about logic errors or data errors. These can be hard to track down if you don't have any tests validating your code.



Good luck!






share|improve this answer













I would narrow down your try:except to only the statement(s) which could throw the error. Having such a large chunk of code in a try:except block could result in many exceptions thrown, none of which would be caught as you're only looking for IOError.



Also, for performance, you should build the statement and the data set from the loop, then throw the statement against cursor.executemany(statement, data) as a single database call instead.



Regarding construction, your code is just one huge blob. It's important that you refactor each operation into its own function, this saves on tracking down errors, and if you make changes in the future, your changes only affect one function. If the change is not liked by the rest of your code, it's easy to back out.

Can you imagine if you made 5 or 10 changes in your program as it is right now, and then it stops working? Which change was the breaking change? How would you find out? Why did it break? etc. Also, I'm not talking just simple syntax errors, I'm also talking about logic errors or data errors. These can be hard to track down if you don't have any tests validating your code.



Good luck!







share|improve this answer













share|improve this answer



share|improve this answer











answered May 2 at 3:39









C. Harley

6615




6615











  • Thanks for the advice ! Does that mean that I should store the json results into an array/list, then when insert into postgres just read from the array/list?
    – Derek
    May 4 at 9:15










  • Perhaps a tuple added to a list? e.g.: [(class, time_, fcst_valid, category), (class, time_, fcst_valid, category), etc]
    – C. Harley
    May 5 at 11:55










  • So if i use psycopg2, just cur.execute(""" insert into table A( class, time_, fcst_valid, category) VALUES ( %s,%s,%s,%s)""",(list)) ?
    – Derek
    May 6 at 20:03
















  • Thanks for the advice ! Does that mean that I should store the json results into an array/list, then when insert into postgres just read from the array/list?
    – Derek
    May 4 at 9:15










  • Perhaps a tuple added to a list? e.g.: [(class, time_, fcst_valid, category), (class, time_, fcst_valid, category), etc]
    – C. Harley
    May 5 at 11:55










  • So if i use psycopg2, just cur.execute(""" insert into table A( class, time_, fcst_valid, category) VALUES ( %s,%s,%s,%s)""",(list)) ?
    – Derek
    May 6 at 20:03















Thanks for the advice ! Does that mean that I should store the json results into an array/list, then when insert into postgres just read from the array/list?
– Derek
May 4 at 9:15




Thanks for the advice ! Does that mean that I should store the json results into an array/list, then when insert into postgres just read from the array/list?
– Derek
May 4 at 9:15












Perhaps a tuple added to a list? e.g.: [(class, time_, fcst_valid, category), (class, time_, fcst_valid, category), etc]
– C. Harley
May 5 at 11:55




Perhaps a tuple added to a list? e.g.: [(class, time_, fcst_valid, category), (class, time_, fcst_valid, category), etc]
– C. Harley
May 5 at 11:55












So if i use psycopg2, just cur.execute(""" insert into table A( class, time_, fcst_valid, category) VALUES ( %s,%s,%s,%s)""",(list)) ?
– Derek
May 6 at 20:03




So if i use psycopg2, just cur.execute(""" insert into table A( class, time_, fcst_valid, category) VALUES ( %s,%s,%s,%s)""",(list)) ?
– Derek
May 6 at 20:03












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193341%2ffetch-parse-and-store-json-in-postgresql%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation