2

I have this URL:

http://example.com?variable=1&variable=2&a=text

I want to transform those values to JSON.

I have tried:

data = json.dumps(request.GET)

But I get {"variable": "2", "a": "text"}

It seems to work for single values but not for multiple ones.

How can I get all the values from the URL transformed?

1

1 Answer 1

3

The urlparse module can do this for you.

Results:

from urlparse import urlparse, parse_qs

o = urlparse('http://example.com?variable=1&variable=2&a=text')
qs = parse_qs(o.query)
print(qs)

results:

{'variable': ['1', '2'], 'a': ['text']}
Sign up to request clarification or add additional context in comments.

4 Comments

For some reason I have this error(anaconda2/lib/python2.7/urlparse.py in urlsplit, line 182): 'function' object has no attribute 'find' So I can't try right now...Looking for a solution to that error.
solved but i got this: {'a': ['1', "2'>>"], 'search_txt': ['T']}
It seems to work. The reason why i got that result seems to be request.get_full_path. It wil return something like: <bound method WSGIRequest.get_full_path of <WSGIRequest: GET '?variable=1&variable=2&a=text'>> Note:I am using django
Solved qs = parse_qs(request.META['QUERY_STRING']) data = json.dumps(qs) result: {"variable": ["1", "2"], "a": ["text"]}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.