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:
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.
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.
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).
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"}'
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With