Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python JSON encoding

I'm trying to encode data to JSON in Python and I been having a quite a bit of trouble. I believe the problem is simply a misunderstanding.

I'm relatively new to Python and never really got familiar with the various Python data types, so that's most likely what's messing me up.

Currently I am declaring a list, looping through and another list, and appending one list within another:

import simplejson, json
data = [['apple', 'cat'], ['banana', 'dog'], ['pear', 'fish']]

x = simplejson.loads(data)
# >>> typeError: expected string or buffer..

x = simplejson.dumps(stream)
# >>> [["apple", "cat"], ["banana", "dog"], ["pear", "fish"]]
# - shouldn't JSON encoded strings be like: {{"apple":{"cat"},{"banana":"dog"}}

So I either:

  • I don't understand JSON Syntax
  • I don't understand the Pythons JSON module(s)
  • I'm using an inappropriate data type.
like image 514
KeyboardInterrupt Avatar asked Jun 11 '09 21:06

KeyboardInterrupt


People also ask

What is encoding JSON Python?

Serializing JSON simply means that you are encoding JSON. It converts the given Python data structure(ex:dict) into its valid JSON object. To handle the data flow in a file, the JSON library in Python uses a dump() and dumps() method, that does the conversion and makes it easy to write data into files.

How do I decode JSON data in Python?

Parse JSON - Convert from JSON to PythonIf you have a JSON string, you can parse it by using the json.loads() method. The result will be a Python dictionary.

What is JSON encoding?

The default encoding is UTF-8, and JSON texts that are encoded in UTF-8 are interoperable in the sense that they will be read successfully by the maximum number of implementations; there are many implementations that cannot successfully read texts in other encodings (such as UTF-16 and UTF-32).


3 Answers

Python lists translate to JSON arrays. What it is giving you is a perfectly valid JSON string that could be used in a Javascript application. To get what you expected, you would need to use a dict:

>>> json.dumps({'apple': 'cat', 'banana':'dog', 'pear':'fish'})
'{"pear": "fish", "apple": "cat", "banana": "dog"}'
like image 113
Paolo Bergantino Avatar answered Oct 19 '22 19:10

Paolo Bergantino


I think you are simply exchanging dumps and loads.

>>> import json
>>> data = [['apple', 'cat'], ['banana', 'dog'], ['pear', 'fish']]

The first returns as a (JSON encoded) string its data argument:

>>> encoded_str = json.dumps( data )
>>> encoded_str
'[["apple", "cat"], ["banana", "dog"], ["pear", "fish"]]'

The second does the opposite, returning the data corresponding to its (JSON encoded) string argument:

>>> decoded_data = json.loads( encoded_str )
>>> decoded_data
[[u'apple', u'cat'], [u'banana', u'dog'], [u'pear', u'fish']]
>>> decoded_data == data
True
like image 36
Mapio Avatar answered Oct 19 '22 19:10

Mapio


In simplejson (or the library json in Python 2.6 and later), loads takes a JSON string and returns a Python data structure, dumps takes a Python data structure and returns a JSON string. JSON string can encode Javascript arrays, not just objects, and a Python list corresponds to a JSON string encoding an array. To get a JSON string such as

{"apple":"cat", "banana":"dog"}

the Python object you pass to json.dumps could be:

dict(apple="cat", banana="dog")

though the JSON string is also valid Python syntax for the same dict. I believe the specific string you say you expect is simply invalid JSON syntax, however.

like image 18
Alex Martelli Avatar answered Oct 19 '22 19:10

Alex Martelli