Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading an existing .json file written out using TinyDB #67

Closed
tomwallis opened this issue Aug 13, 2015 · 5 comments
Closed

Reading an existing .json file written out using TinyDB #67

tomwallis opened this issue Aug 13, 2015 · 5 comments
Labels
Milestone

Comments

@tomwallis
Copy link

Apologies if this is a stupid question, but I have not seen the answer in your docs.

A colleague has passed me a .json file written out using TinyDB. I would like to read its contents. However, if I use

db = TinyDB('my_file.json')

then db is empty, and the file on disk is overwritten (contains one line {"_default": {}}). What's the correct way to read this file into a Python interpreter? I see no "load" method in the docs and tinydb.Storage.read doesn't do this.

@eugene-eeo
Copy link
Contributor

Slightly weird question but is the JSON file valid? The last time I worked on the code we used a try..except block to test if files were empty, so if the file was manually modified and some minor issues were present (think missing quotes, unbalanced parens etc) then it would result in the same case as an empty file. So try loading it manually using json.loads to see if it works.

@tomwallis
Copy link
Author

Ah, you seem to have it right.

import json
with open('my_file.json', 'r') as f:
    data = json.load(f)

/Users/user/miniconda3/envs/default/lib/python3.4/json/decoder.py in raw_decode(self, s, idx)
    357       
    358         try:
--> 359             obj, end = self.scan_once(s, idx)
    360         except StopIteration as err:
    361             raise ValueError(errmsg("Expecting value", s, err.value)) from None

ValueError: Expecting ',' delimiter: line 1 column 12582912 (char 12582911)

The .json file my colleague gave me appears to be invalid. Issue solved, but I suppose it would be helpful if tinyDB reported an error rather than silently overwriting the file.

@msiemens
Copy link
Owner

Actually I'd like to keep this open. TinyDB currently assumes that if the JSON module raises a ValueError, the database file was empty. But as in this case, that's a pretty shaky assumption and something I want to revisit for a potential v3.0.

@msiemens msiemens reopened this Aug 14, 2015
@eugene-eeo
Copy link
Contributor

Unhelpful but encouraging 👍 for looking into this issue.

@msiemens msiemens added this to the v3.0 milestone Aug 17, 2015
@msiemens msiemens added the bug label Aug 17, 2015
@msiemens msiemens mentioned this issue Sep 17, 2015
Merged
9 tasks
@msiemens
Copy link
Owner

This should be closed in the 3.0 release, I just forgot to close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants