Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Json API
#1
Exclamation 
Hi All,
I have written the following code to generate json file from oracle database, and the file doesn't have the Keys(Columns) only values printing in the file. Also no curly braces. Below is the output too. Please assit me on this.
import oracledb
import datetime
import json
try:
    oracledb.init_oracle_client()
    con = oracledb.connect(connectioninfo)
except oracledb.DatabaseError as er:
    print('There is an error in the Oracle database:', er)

else:
    try:
        cur = con.cursor()

        # fetchall() is used to fetch all records from result set
        cur.execute('select emp_no,first_name,salary,dept_no from emp')
        rows = cur.fetchall()
        print(rows)
        save_file = open("emp.json", "w")  
        json.dump(rows, save_file, indent = 6)  
        save_file.close()
        with open("emp.json") as j_out:
            saved_data = json.load(j_out)
            print(saved_data)
    except oracledb.DatabaseError as er:
        print('There is an error in the Oracle database:', er)

    except Exception as er:
        print('Error:'+str(er))

    finally:
        if cur:
            cur.close()
finally:
    if con:
        con.close()
Output:
Output: [ [ 1, "Jay", 24000.0, 10 ], [ 2, "Rn", 34000.0, 10 ] }
Thanks
Jay
deanhystad write Mar-02-2024, 08:59 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Messages In This Thread
Json API - by JayPy - Mar-02-2024, 08:25 PM
RE: Json API - by deanhystad - Mar-02-2024, 09:32 PM
RE: Json API - by JayPy - Mar-04-2024, 12:55 PM
RE: Json API - by buran - Mar-03-2024, 05:18 AM
RE: Json API - by deanhystad - Mar-04-2024, 04:28 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020