Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can't use urlencode to encode json format data?

I have a problem about urlencode in python 2.7:

>>> import urllib
>>> import json
>>> urllib.urlencode(json.dumps({'title':"hello world!",'anonymous':False,'needautocategory':True}))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/urllib.py", line 1280, in urlencode
    raise TypeError
TypeError: not a valid non-string sequence or mapping object
like image 253
firezhang Avatar asked Nov 28 '11 08:11

firezhang


People also ask

How do I encode a JSON string?

Stringify a JavaScript ObjectUse the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.

Is Urlencode necessary?

Why do we need to encode? URLs can only have certain characters from the standard 128 character ASCII set. Reserved characters that do not belong to this set must be encoded. This means that we need to encode these characters when passing into a URL.

How do I use python Urlencode?

In Python 3+, You can URL encode any string using the quote() function provided by urllib. parse package. The quote() function by default uses UTF-8 encoding scheme.

What is JSON encoding?

JSON (JavaScript Object Notation, pronounced /ˈdʒeɪsən/; also /ˈdʒeɪˌsɒn/) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays (or other serializable values).


3 Answers

urlencode can encode a dict, but not a string. The output of json.dumps is a string.

Depending on what output you want, either don't encode the dict in JSON:

>>> urllib.urlencode({'title':"hello world!",'anonymous':False,'needautocategory':True})
'needautocategory=True&anonymous=False&title=hello+world%EF%BC%81'

or wrap the whole thing in a dict:

>>> urllib.urlencode({'data': json.dumps({'title':"hello world!",'anonymous':False,'needautocategory':True})})
'data=%7B%22needautocategory%22%3A+true%2C+%22anonymous%22%3A+false%2C+%22title%22%3A+%22hello+world%5Cuff01%22%7D'

or use quote_plus() instead (urlencode uses quote_plus for the keys and values):

>>> urllib.quote_plus(json.dumps({'title':"hello world!",'anonymous':False,'needautocategory':True}))
'%7B%22needautocategory%22%3A+true%2C+%22anonymous%22%3A+false%2C+%22title%22%3A+%22hello+world%5Cuff01%22%7D'
like image 185
Petr Viktorin Avatar answered Oct 30 '22 13:10

Petr Viktorin


Because urllib.urlencode "converts a mapping object or a sequence of two-element tuples to a “percent-encoded” string...". Your string is neither of these.

I think you need urllib.quote or urllib.quote_plus.

like image 23
DrTyrsa Avatar answered Oct 30 '22 13:10

DrTyrsa


For those of ya'll getting the error:

AttributeError: module 'urllib' has no attribute 'urlencode'

It's because urllib has been split up in Python 3


Here's the code for Python 3:

import urllib.parse

data = {
    "title": "Hello world",
    "anonymous": False,
    "needautocategory": True
}

urllib.parse.urlencode(data)
# 'title=Hello+world&anonymous=False&needautocategory=True'
like image 21
John R Perry Avatar answered Oct 30 '22 12:10

John R Perry