Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have JSON data stored in the variable data.

I want to write this to a text file for testing so I don't have to grab the data from the server each time.

Currently, I am trying this:

obj = open('data.txt', 'wb')
obj.write(data)
obj.close

And am receiving the error:

TypeError: must be string or buffer, not dict

How to fix this?

share|improve this question
    
See json docs – Martin Thoma Oct 11 '15 at 14:44
up vote 490 down vote accepted

You forgot the actual JSON part - data is a dictionary and not yet JSON-encoded. Write it like this:

import json
with open('data.txt', 'w') as outfile:
    json.dump(data, outfile)

Note: Works on both 3.x and 2.x .

share|improve this answer
2  
this might be helpful for serializing: stackoverflow.com/questions/4512982/… – jedierikb Feb 11 '13 at 17:27
3  
Do you mean json.dump or json.dumps? – TerminalDilettante Aug 13 '15 at 14:46
15  
@TerminalDilettante json.dump writes to a file or file-like object, whereas json.dumps returns a string. – phihag Aug 13 '15 at 20:58
3  
btw: to re read the data use: with open('data.txt') as infile: d = json.load(infile). See: this answer – klaas Mar 7 at 12:59
1  
@denvar No, this answer is finely tuned. On Python 3, json.dump writes to a text file, not a binary file. You'd get a TypeError if the file was opened with wb. On older Python versions, both w nand wb work. An explicit encoding is not necessary since the output of json.dump is ASCII-only by default. If you can be sure that your code is never run on legacy Python versions and you and the handler of the JSON file can correctly handle non-ASCII data, you can specify one and set ensure_ascii=False. – phihag Apr 19 at 18:47

To get utf8-encoded file (for smaller size) in Python 2.x:

import io, json
with io.open('data.txt', 'w', encoding='utf-8') as f:
  f.write(unicode(json.dumps(data, ensure_ascii=False)))

For example for data = {u'абвгд': 1} I get 17 bytes for utf8 vs 26 for ascii version.

The code is simpler in Python 3.x:

import json
with open('data.txt', 'w') as f:
  json.dump(data, f, ensure_ascii=False)
share|improve this answer
1  
The unicode is superfluous - the result of json.dumps is already a unicode object. Note that this fails in 3.x, where this whole mess of output file mode has been cleaned up, and json always uses character strings (and character I/O) and never bytes. – phihag Feb 14 '13 at 11:20
3  
In 2.x type(json.dumps('a')) is <type 'str'>. Even type(json.dumps('a', encoding='utf8')) is <type 'str'>. – Antony Hatchkins Feb 14 '13 at 11:25
2  
Yes, in 3.x json uses strings, yet the default encoding is ascii. You have to explicitly tell it that you want utf8 even in 3.x. Updated the answer. – Antony Hatchkins Feb 14 '13 at 11:39
3  
Oh, you're totally right - I must have confused something. +1 for the detail. – phihag Feb 14 '13 at 14:52
1  
This is the anwer that works, not the other ones. – Stephan Kristyn Oct 13 '14 at 23:18

I would answer with slight modification with aforementioned answers and that is to write a prettified JSON file which human eyes can read better. For this, pass sort_keys as True and indent with 4 space characters and you are good to go. Also take care of ensuring that the ascii codes will not be written in your JSON file:

with open('data.txt', 'w') as outfile:
     json.dump(jsonData, outfile, sort_keys = True, indent = 4,
ensure_ascii=False)
share|improve this answer
6  
+1 prettified JSON is really useful for testing purposes – Hari Ganesan Mar 21 '14 at 16:00
2  
still getting UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' – Stephan Kristyn Oct 13 '14 at 23:16
1  
@SirBenBenji Ensure the string you are trying to write to follow: str.decode('utf-8'). – ambodi Apr 22 '15 at 9:08
1  
@SirBenBenji You can try using codecs too, as dinos66 specifies below – Shiv Sep 3 '15 at 19:01
    
You also have to declare your encoding by adding # -*- coding: utf-8 -*- after the shebang – aesede Apr 2 at 17:29

For those of you who are trying to dump greek or other "exotic" languages such as me but are also having problems (unicode errors) with weird characters such as the peace symbol (\u262E) or others which are often contained in json formated data such as Twitter's, the solution could be as follows (sort_keys is obviously optional):

import codecs, json
with codecs.open('data.json', 'w', 'utf8') as f:
     f.write(json.dumps(data, sort_keys = True, ensure_ascii=False))
share|improve this answer

I don't have enough reputation to add in comments, so I just write some of my findings of this annoying TypeError here:

Basically, I think it's a bug in the json.dump() function in Python 2 only - It can't dump a Python (dictionary / list) data containing non-ASCII characters, even you open the file with the encoding = 'utf-8' parameter. (i.e. No matter what you do). But, json.dumps() works on both Python 2 and 3.

To illustrate this, following up phihag's answer: the code in his answer breaks in Python 2 with exception TypeError: must be unicode, not str, if data contains non-ASCII characters. (Python 2.7.6, Debian):

import json
data = {u'\u0430\u0431\u0432\u0433\u0434': 1} #{u'абвгд': 1}
with open('data.txt', 'w') as outfile:
    json.dump(data, outfile)

It however works fine in Python 3.

Antony Hatchkins's first solution works in Python 2, but as a follow up to his answer: The unicode part is not necessary (and IMHO, it's wrong), the following works on my system (ensure_ascii=Fasle is also not strictly needed for the code to work, but more related to the output format):

import io, json
with io.open('data.txt', 'w', encoding='utf-8') as f:
  f.write(json.dumps(data, ensure_ascii=False))

In fact, the above code work in both Python 2 and 3 (the original code with unicode() call won't work in Python 3 because unicode() function no longer exists in Python 3). I guess the reason both code (with / without unicode() call) work in Python 2 is because Python 2 uses bytes while to write on the file handler, and unicode is converted to bytes implicitly before writing.

share|improve this answer
json.dump(data, open('data.txt', 'wb'))
share|improve this answer

Write a data in file using JSON use json.dump() or json.dumps() used. write like this to store data in file.

import json
data = [1,2,3,4,5]
with open('no.txt', 'w') as txtfile:
    json.dump(data, txtfile)

this example in list is store to a file.

share|improve this answer

Just like phiphags answer, but with nice formatting:

import json
with open('data.txt', 'w') as outfile:
    json.dump(data, outfile, indent=4, sort_keys=True, separators=(',', ':'))

Explanation of the parameters of json.dump:

  • indent: Use 4 spaces to indent each entry, e.g. when a new dict is started (otherwise all will be in one line),
  • sort_keys: sort the keys of dictionaries
  • separators: To prevent Python from adding trailing whitespaces
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.