malaba
malaba

Reputation: 23

Trying to read my .json data in python but keep getting an error

I have a simple code to be able to read my json data in python and convert to dictionary so I can use it for sqlite. However, I keep running into this error.

Code I ran:

import json

with open("users.json", "r") as f:
    json_str = f.read()
    json_value = json.loads(json_str)
    print(type(json_value))

I have a set of data like this:

{"_id":{"$od":"5f12"},"ready":true,"createdon":{"$date":1609687444800},"lastLogin":{"$date":1858},"position":"consumer","signUp":"Email","state":"WI"}
{"_id":{"$od":"5f12"},"ready":true,"createdon":{"$date":1609687444800},"lastLogin":{"$date":1858},"position":"consumer","signUp":"Email","state":"WI"}

The error I get:

    Traceback (most recent call last):
  File "json_to_sqlite.py", line 5, in <module>
    json_value = json.loads(json_str)
  File "/Users/malaba/opt/anaconda3/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/Users/malaba/opt/anaconda3/lib/python3.8/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 186)

Upvotes: 0

Views: 136

Answers (1)

Lucas Roy
Lucas Roy

Reputation: 133

@rv.kvetch is right, the problem is that your users.json file has two distinct json strings, i.e. you have two dictionaries stored in your file when you can have at most one. An easy way to fix this is to wrap the dictionaries in a list (hence, you have one json object that holds all your other objects). The newly formatted users.json file would look something like this:

[{"_id":{"$od":"5f12"},"ready":true,"createdon":{"$date":1609687444800},"lastLogin":{"$date":1858},"position":"consumer","signUp":"Email","state":"WI"},
{"_id":{"$od":"5f12"},"ready":true,"createdon":{"$date":1609687444800},"lastLogin":{"$date":1858},"position":"consumer","signUp":"Email","state":"WI"}]

Please make note of the opening and closing braces and the extra comma separating the dictionaries in your new file. Reading the json file would be the same procedure as before, but if you wanted to pull a specific dictionary out of your returned json, you would need to index the list accordingly. More details on the json module can be found here.

EDIT:

If you have a large data file, this sort of operation is not feasible to do manually so you will have to get clever to look for additional structure to the file. For example, if you know that each dictionary in the file is separated by a newline and there are no other newline characters in your file (similar to the example you provided), then you can automatically do this conversion like so:

import json

with open("users.json", "r") as f:
    new = [json.loads(x) for x in f.read().splitlines()]

with open("users.json", "w") as f:
    json.dump(new, f)

Upvotes: 1

Related Questions