Monday, August 26, 2013

Json makes tuples to list

While working on json format, came across something interesting.

sample_list  = [(1,2),(3,4)]
print json.dumps(sample_list)  # [[1,2],[3,4]]

As we can see here, json isnt preserving tuples. On searching, I got to know, JSON doesnt understand tuples. These are the basic data types for json(from Wikipedia )

JSON's basic types are:
  • Array (an ordered sequence of values, comma-separated and enclosed in square brackets; the values do not need to be of the same type)
  • Object (an unordered collection of key:value pairs with the ':' character separating the key and the value, comma-separated and enclosed in curly braces; the keys must be strings and should be distinct from each other)
Here are some solutions for the same on stackoverflow.

Cheers..!!

No comments:

Post a Comment