1

Currently I try to convert data from an ESRI shapefile (.shp) to a Json file using the json package.

In this process, I want to convert a dictionairy containing the coordinates of a lot of different points:

json.dumps({"Points" : coordinates}) 

The list "coordinates" looks like:

[[-2244.677490234375, -3717.6876220703125], [-2252.7623006509266, -3717.321774721159], 
..., [-2244.677490234375, -3717.6876220703125]]

and contains about several hundreds of coordinate pairs.

However, when I try to execute json.dumps, I get the following error:

[-2244.677490234375, -3717.6876220703125] is not JSON serializable

My first thought was, that it can not handle decimal/float values But if I execute the following working example containing only two of the coordinate pairs:

print(json.dumps({"Points" :  [[-2244.677490234375, -3717.6876220703125],
[-2244.677490234375, -3717.6876220703125]]}))

tt works and I do not get an error... The output in this case is:

{"Points": [[-2244.677490234375, -3717.6876220703125], [-2244.677490234375, -3717.6876220703125]]}

I don't get why it isn't working with my "coordinates"-list.

Jannik
  • 13
  • 2

1 Answers1

1

The error you are seeing most commonly happens with custom classes. So I believe your problem has to do with the way pyshp is supplying the coordinate values. I can't be sure without seeing your code, but looking at the pyshp source I found an _Array class that is used in a few places.

class _Array(array.array):
  """Converts python tuples to lits of the appropritate type.
  Used to unpack different shapefile header parts."""
  def __repr__(self):
    return str(self.tolist())

The __repr__ might explain why you believe you are seeing a standard list or tuple when in fact it is a custom class. I put together a python fiddle which demonstrates the exception when providing pyshp's _Array class to json.dumps().

To fix this issue you should pass coordinates.tolist() to your dumps call.

json.dumps({"Points" : coordinates.tolist()}) 
craigts
  • 2,857
  • 1
  • 25
  • 25
  • Thank you very much!! Now it is working, but I still do not understand the previous proplem completely... My coordinates actually were lists.. So when I tried to apply .tolist(), an error occured, saying that lists do not have the method tolist(). So I converted my lists to numpy arrays, which I than converted back to lists using .tolist()... And strangely, now it is working :-D – Jannik Oct 28 '16 at 15:30
  • Ah excellent. There definitely was some issue with the objects... I guess we'll never know :) – craigts Oct 28 '16 at 15:36
  • BTW I think you had a list of the numpy arrays, but not 100% sure. Also please remember to mark mine as the answer. – craigts Oct 28 '16 at 15:48