Python JSON Encoding

I am trying to encode data in Python JSON and I am having a little problem. I believe that the problem is simply a misunderstanding.

I'm relatively new to Python and never familiar with the various types of Python data, so it most likely scares me.

I am currently declaring a list, iterating over a loop and another list, and adding one list to 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 me too:

  • I do not understand JSON Syntax
  • I don't understand the python json module
  • I am using an unacceptable data type.
+46
json python types encoding simplejson
Jun 11 '09 at 21:37
source share
7 answers

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

 >>> json.dumps({'apple': 'cat', 'banana':'dog', 'pear':'fish'}) '{"pear": "fish", "apple": "cat", "banana": "dog"}' 
+56
Jun 11 '09 at 21:41
source share

I think you just exchange landfills and cargo.

 >>> 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 data corresponding to its string argument (JSON):

 >>> decoded_data = json.loads( encoded_str ) >>> decoded_data [[u'apple', u'cat'], [u'banana', u'dog'], [u'pear', u'fish']] >>> decoded_data == data True 
+18
Jun 11 '09 at 23:07
source share

In simplejson (or in the json library 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. A 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") 

although the JSON string is also valid for Python syntax for the same dict . I believe that the specific line you say you expect is just the invalid JSON syntax.

+17
Jun 11 '09 at 21:41
source share

The data you encode is a keyless array, so JSON encodes it with [] brackets. See www.json.org for more information. Curly braces are used for lists with key / value pairs.

From www.json.org:

JSON is built on two structures:

A collection of name / value pairs. In different languages, this is implemented as an object, record, structure, dictionary, hash table, key list, or associative array. An ordered list of values. In most languages, this is implemented as an array, vector, list, or sequence.

An object is an unordered set of name / value pairs. An object starts with {(left bracket) and ends} (right bracket). Each name is followed by: (colon) and the name / value pairs are separated by a sign (comma).

An array is an ordered set of values. The array begins with [(left bracket) and ends with] (right bracket). Values ​​are separated, (Comma).

+4
Jun 11 '09 at 21:41
source share

JSON uses square brackets for lists ( [ "one", "two", "three" ] ) and braces for keyword / value dictionaries (also called objects in JavaScript, {"one":1, "two":"b"} ).

The dump is quite correct, you get a list of three elements, each of which is a list of two lines.

if you need a dictionary, maybe something like this:

 x = simplejson.dumps(dict(data)) >>> {"pear": "fish", "apple": "cat", "banana": "dog"} 

expected string (' {{"apple":{"cat"},{"banana":"dog"}} ') is not valid JSON. BUT

+3
Jun 11 '09 at 21:45
source share

So simplejson.loads takes a json string and returns a data structure, so you get a type error there.

simplejson.dumps (data) returns with

 '[["apple", "cat"], ["banana", "dog"], ["pear", "fish"]]' 

What is the json array you need since you gave it a python array.

If you want to get syntax like "object", you have to do

 >>> data2 = {'apple':'cat', 'banana':'dog', 'pear':'fish'} >>> simplejson.dumps(data2) '{"pear": "fish", "apple": "cat", "banana": "dog"}' 

which is javascript will exit as an object.

+3
Jun 11 '09 at 21:46
source share

Try:

 import simplejson data = {'apple': 'cat', 'banana':'dog', 'pear':'fish'} data_json = "{'apple': 'cat', 'banana':'dog', 'pear':'fish'}" simplejson.loads(data_json) # outputs data simplejson.dumps(data) # outputs data_joon 

NB: Based on Paolo's answer.

+3
Jun 11 '09 at 21:46
source share



All Articles