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
Labels
bug
Milestone

Comments

@tomwallis
Copy link

@tomwallis tomwallis commented Aug 13, 2015

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

This comment has been minimized.

Copy link
Contributor

@eugene-eeo eugene-eeo commented Aug 13, 2015

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

This comment has been minimized.

Copy link
Author

@tomwallis tomwallis commented Aug 14, 2015

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.

@tomwallis tomwallis closed this Aug 14, 2015
@msiemens

This comment has been minimized.

Copy link
Owner

@msiemens msiemens commented Aug 14, 2015

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

This comment has been minimized.

Copy link
Contributor

@eugene-eeo eugene-eeo commented Aug 14, 2015

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 referenced this issue Sep 17, 2015
Merged
9 of 9 tasks complete
msiemens added a commit that referenced this issue Sep 17, 2015
@msiemens

This comment has been minimized.

Copy link
Owner

@msiemens msiemens commented Nov 29, 2015

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

@msiemens msiemens closed this Nov 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.