Skip to content

Simplify json load/dump usage #118

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

Merged
merged 1 commit into from
Sep 1, 2022

Conversation

evanrrees
Copy link
Contributor

When reading / writing a file, the json.load() and json.dump() methods can be used to avoid an unnecessary call to f.read() or f.write().

@netlify
Copy link

netlify bot commented Aug 31, 2022

‼️ Deploy request for pythoncheatsheet rejected.

Name Link
🔨 Latest commit 1efbf92

@wilfredinni
Copy link
Owner

Hi, thank you for contributing. The thing is that you need to use the read method to load content from a file, or you would get this error:

>>> with open("filename.json", "r") as f:
...     json_content = json.loads(f)
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/lib/python3.8/json/__init__.py", line 341, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper

@evanrrees
Copy link
Contributor Author

Hi, thank you for contributing. The thing is that you need to use the read method to load content from a file, or you would get this error:

>>> with open("filename.json", "r") as f:
...     json_content = json.loads(f)

@wilfredinni Note that json.load and json.loads are different methods.

@wilfredinni
Copy link
Owner

You are absolutely right!

@wilfredinni wilfredinni merged commit e8d557d into wilfredinni:master Sep 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants