Skip to content

json.dump: fp must be a text file object #76539

Closed

Description

@qingyunha
mannequin
BPO 32358
Nosy @berkerpeksag, @qingyunha

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2018-03-23.21:27:27.992>
created_at = <Date 2017-12-18.06:24:46.414>
labels = ['invalid', 'library']
title = 'json.dump: fp must be a text file object'
updated_at = <Date 2018-03-23.21:27:27.981>
user = 'https://github.com/qingyunha'

bugs.python.org fields:

activity = <Date 2018-03-23.21:27:27.981>
actor = 'berker.peksag'
assignee = 'none'
closed = True
closed_date = <Date 2018-03-23.21:27:27.992>
closer = 'berker.peksag'
components = ['Library (Lib)']
creation = <Date 2017-12-18.06:24:46.414>
creator = 'qingyunha'
dependencies = []
files = []
hgrepos = []
issue_num = 32358
keywords = []
message_count = 2.0
messages = ['308517', '314332']
nosy_count = 2.0
nosy_names = ['berker.peksag', 'qingyunha']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue32358'
versions = ['Python 3.6']

Activity

  1. qingyunha commented on Dec 18, 2017

    qingyunhamannequin
    MannequinAuthor
    >>> import json        
    >>> f = open('/tmp/t.json', 'wb')              
    >>> json.dump(123, f)  
    Traceback (most recent call last):             
      File "<stdin>", line 1, in <module>          
      File "/usr/lib/python3.6/json/__init__.py", line 180, in dump                               
        fp.write(chunk)    
    TypeError: a bytes-like object is required, not 'str'
    

    This may not a bug. But it should mention at docs https://docs.python.org/3/library/json.html#json.dump

  2. added
    stdlib
    on Dec 18, 2017
  3. berkerpeksag commented on Mar 23, 2018

    @berkerpeksag
    Member

    This is already documented in the json.dump() documentation:

    The json module always produces str objects, not bytes objects.
    Therefore, fp.write() must support str input.
    

    Note that the traceback you've posted doesn't have anything to do with the json module and it's expected:

        >>> f = open('/tmp/t.json', 'wb')
        >>> f.write('foo')
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        TypeError: a bytes-like object is required, not 'str'
  4. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions