How to insert all rows from JSON_Object to SQL Table using Python
Question:
I am new to Python and APIs. Data is a result of API response.
I have a json_object like below.
json_object = json.dumps(data,indent=2)
which has data like below. I successfully connected to SQL server localhost using pymssql but I need help in extracting all the rows from this json to sql table (Table_A).
I tried referring various posts but they are selecting the individual columns from json. But I may have 20 plus columns from json result. So assume that I have a table with all those 20+ columns (Table A).
How to send this data to ms sql server?
[
{
"Key": "ITSAMPLE-8142",
"Summary": "Test Issue,Data.Analystz.111,Data.Analystz.111",
"Status": "Open"
},
{
"Key": "ITSAMPLE-8141",
"Summary": "Test Issue,Data.Analystz.111,Data.Analystz.111",
"Status": "Open"
}
]
Answers:
You can use the pandas
library to convert the JSON object into a DataFrame and then insert the DataFrame into your SQL Server database using pymssql
.
Try this,
import pandas as pd
import pymssql
# Convert JSON object to DataFrame
df = pd.read_json(json_object)
# Connect to SQL Server database
conn = pymssql.connect(
server='localhost',
user='your_username',
password='your_password',
database='your_database'
)
# Insert DataFrame into SQL Server table
df.to_sql('Table_A', conn, if_exists='append', index=False)
# Close the database connection
conn.close()
Here, if_exists='append'
specifies that the data should be appended to the existing Table_A
table in the database. If the table doesn’t exist yet, it will be created. index=False
specifies that the index column of the DataFrame should not be included in the database table & you may need to adjust the connection parameters (server, user, password, and database)
according to your specific SQL Server setup.
I am new to Python and APIs. Data is a result of API response.
I have a json_object like below.
json_object = json.dumps(data,indent=2)
which has data like below. I successfully connected to SQL server localhost using pymssql but I need help in extracting all the rows from this json to sql table (Table_A).
I tried referring various posts but they are selecting the individual columns from json. But I may have 20 plus columns from json result. So assume that I have a table with all those 20+ columns (Table A).
How to send this data to ms sql server?
[
{
"Key": "ITSAMPLE-8142",
"Summary": "Test Issue,Data.Analystz.111,Data.Analystz.111",
"Status": "Open"
},
{
"Key": "ITSAMPLE-8141",
"Summary": "Test Issue,Data.Analystz.111,Data.Analystz.111",
"Status": "Open"
}
]
You can use the pandas
library to convert the JSON object into a DataFrame and then insert the DataFrame into your SQL Server database using pymssql
.
Try this,
import pandas as pd
import pymssql
# Convert JSON object to DataFrame
df = pd.read_json(json_object)
# Connect to SQL Server database
conn = pymssql.connect(
server='localhost',
user='your_username',
password='your_password',
database='your_database'
)
# Insert DataFrame into SQL Server table
df.to_sql('Table_A', conn, if_exists='append', index=False)
# Close the database connection
conn.close()
Here, if_exists='append'
specifies that the data should be appended to the existing Table_A
table in the database. If the table doesn’t exist yet, it will be created. index=False
specifies that the index column of the DataFrame should not be included in the database table & you may need to adjust the connection parameters (server, user, password, and database)
according to your specific SQL Server setup.