Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (20.3k points)

What am I missing? I want to dump a dictionary as a json string.

I am using python 2.7

With this code:

import json

fu = {'a':'b'}

output = json.dump(fu)

I get the following error:

Traceback (most recent call last):

  File "/usr/local/lib/python2.7/dist-packages/gevent-1.0b2-py2.7-linux-x86_64.egg/gevent/greenlet.py", line 328, in run

    result = self._run(*self.args, **self.kwargs)

  File "/home/ubuntu/workspace/bitmagister-api/mab.py", line 117, in mabLoop

    output = json.dump(fu)

TypeError: dump() takes at least 2 arguments (1 given)

<Greenlet at 0x7f4f3d6eec30: mabLoop> failed with TypeError

2 Answers

0 votes
by (40.7k points)

You can try using json.dumps to dump str like this:

>>> import json

>>> json.dumps({'a':'b'})

'{"a": "b"}'

json.dump dumps to a file

0 votes
by (106k points)

You should use json.dump:-

json.dumps(fu)

Related questions

0 votes
1 answer
asked Jul 15, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Dec 12, 2020 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Dec 10, 2020 in Python by laddulakshana (16.4k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...