Error: Set not JSON Serializable while converting TSV File into JSON Format using Python
Question: I'm looking to convert a TSV File I have into JSON Format for mapping (google
fusion maps doesn't support mapping multiple objects in the same location so
I'm converting it to JSON format to try on Mapbox). Here's my TSV file if
you're curious:

<https://github.com/yongcho822/Movies-in-the-
park/blob/master/MovieParksGeocodeTest.tsv>

And here is my corresponding python code thus far:

    
    
    import json
    import csv
    
    def create_map(datafile):
        geo_map = {"type":"FeatureCollection"}
        item_list = []
        with open(datafile, 'r') as tsvfile:
            reader = csv.DictReader(tsvfile, delimiter = '\t')
    
            for i, line in enumerate(reader):
                data = {}
                data['type'] = 'Feature'
                data['id'] = i
                data['properties']={'title': line['Movie Title'],
                                   'description': line['Amenities'],
                                   'date': line['Date']}
                data['name'] = {line['Location']}
                data['geometry'] = {'type':'Point',
                                   'coordinates':(line['Lat'], line['Lng'])}
                item_list.append(data)
            #print item_list
            for point in item_list:
                geo_map.setdefault('features', []).append(point)
            print 'CHECKPOINT'
            with open("thedamngeojson.geojson", 'w') as f:
                f.write(json.dumps(geo_map))
    
    create_map('MovieParksGeocodeTest.tsv')
    

It's throwing me an error at the end (after it prints CHECKPOINT), saying

> TypeError: set(['Edgebrook Park, Chicago ']) is not JSON serializable

I figure the last two lines is where the error is.. but what's wrong and how
do I fix it??


Answer: [JSON](http://www.json.org/) is designed to be a very simple, very portable
format; the only kinds of values it understands are strings, numbers,
booleans, null (like Python `None`), `object`s (like a Python `dict`) and
`array`s (like a Python `list`).

But at least one of the values in your dictionary is a `set`:

    
    
    data['name'] = {line['Location']}
    

Since JSON doesn't have a `set` type, you get an error telling you that `set …
is not JSON serializable`.

If you don't actually _need_ this to be a `set` instead of a `list` (which you
probably don't—if it really only ever have one element, who cares which
collection type it is?), the easy answer is to change it to be a `list`:

    
    
    data['name'] = [line['Location']]
    

(In fact, even when you _do_ need this to be a set during processing, you
usually don't need it to be a set during storage/interchange. If the consumer
of your file needs to use it as a set, it can always convert it back later.)

