1

I am trying to import data from a json file to sqlite but I got some error, I thought first that the problem is in the paths, I changed them in many ways but... no result, it gives me this error:

Traceback (most recent call last):
  File "C:/Users/Taner/PycharmProjects/untitled/MyProgram.py", line 13, in <module>
    json_object = json.loads(dummy_json)
  File "C:\Users\Taner\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'

and here is my code:

import json
import bz2
import sqlite3

fpath = "C:\\Users\\Taner\\Downloads\\RC_2012-12.bz2"
databasepath = "C:\\Users\\Taner\\Desktop\\Seagate\\reddit"

conn = sqlite3.connect(databasepath)
curs = conn.cursor()
with bz2.BZ2File(fpath) as file:
    for line in file:
        dummy_json = line
        json_object = json.loads(dummy_json)
        po = json.loads(line.decode('utf8'))
        curs.execute("INSERT INTO Reddit VALUES (?,?,?)", (po['id'], po['subreddit_id'], po['subreddit'],))
        conn.commit()

2 Answers 2

1

The correct form of your loop should be:

with bz2.BZ2File(fpath) as file:
    for line in file:
        po = json.loads(json.loads(line.decode('utf8')))
        curs.execute("INSERT INTO Reddit VALUES (?,?,?)", (po['id'], po['subreddit_id'], po['subreddit'],))
        conn.commit()
Sign up to request clarification or add additional context in comments.

Comments

1

You already have the answer here, since in the line following the error you correctly decode the line from utf-8 before passing it to json.loads. The line with the error is therefore completely pointless, and you should remove it.

Comments

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.