Type Error outputting to geojson with csvjson
Created by: clhenrick
I'm getting the error
TypeError: Decimal('9415846') is not JSON serializable
when attempting to output GeoJSON using csvjson
when my input file has a field / property that is an integer. This error does not happen when running csvjson
when not passing the lat
and lon
flags, though I notice the value for the field (stnid
) gets a decimal appended to it.
the csvkit command (for example, csvstat -v test.csv)
cat data.csv | csvjson -v --lat lat --lon lng
the input file (rename the .csv to .txt for GitHub)
data.csv / data.txt
(attached to this issue but also included below):
data.txt
name,stnid,lat,lng
"CLARKSBURG, ACRAMENTO RIVER",9415846,38.4167,-121.523
Bodega Harbor entrance,9415625,38.3083,-123.055
"NAPA, NAPA RIVER",9415623,38.2983,-122.28
SNODGRASS SLOUGH,9415565,38.2767,-121.495
"SUISUN CITY, SUISUN SLOUGH",9415498,38.2367,-122.03
the output text (including the traceback)
$ cat data.csv | csvjson -v --lat lat --lon lng
{"type": "FeatureCollection", "bbox": [-123.055, 38.2367, -121.495, 38.4167], "features": [{"type": "Feature", "geometry": {"type": "Point", "coordinates": [-121.523, 38.4167]}, "properties": {"name": "CLARKSBURG, ACRAMENTO RIVER", "stnid": Traceback (most recent call last):
File "/usr/local/bin/csvjson", line 11, in <module>
sys.exit(launch_new_instance())
File "/usr/local/lib/python2.7/site-packages/csvkit/utilities/csvjson.py", line 192, in launch_new_instance
utility.run()
File "/usr/local/lib/python2.7/site-packages/csvkit/cli.py", line 113, in run
self.main()
File "/usr/local/lib/python2.7/site-packages/csvkit/utilities/csvjson.py", line 161, in main
dump_json(output)
File "/usr/local/lib/python2.7/site-packages/csvkit/utilities/csvjson.py", line 63, in dump_json
json.dump(data, stream, default=default, **json_kwargs)
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 189, in dump
for chunk in iterable:
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 434, in _iterencode
for chunk in _iterencode_dict(o, _current_indent_level):
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
for chunk in chunks:
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 332, in _iterencode_list
for chunk in chunks:
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
for chunk in chunks:
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
for chunk in chunks:
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 442, in _iterencode
o = _default(o)
File "/usr/local/lib/python2.7/site-packages/csvkit/utilities/csvjson.py", line 60, in default
raise TypeError('%s is not JSON serializable' % repr(obj))
TypeError: Decimal('9415846') is not JSON serializable
the Python version (python --version)
$ python --version
Python 2.7.13
Huge thanks to you all for writing and maintaining this software!