0

I extracted comments made in a website by converting the POST payload from the website to JSON and extracting the value in the comments field in Python.

The JSON looks something like this:

{
    .
    .
    "comment": "The Quick Brown Fox Jumped Over The Lazy Dog"
    .
    .
}

When I print the comment using the print() function, this is how the comment is printed: The+Quick+Brown+Fox+Jumped+Over+The+Lazy+Dog

The type of the comment seems to be 'unicode'. I have tried everything mentioned here and here but those don't solve my problem.

I am using python 2.7. Right now I am printing it for debugging purposes but I want to store it in a database later. So I would need a permanent solution to convert unicode to string.

EDIT: I use var = json.loads(payload_from_POST) to convert the POST payload to JSON. Then I extract the value of "comment" by var['comment'].

Community
  • 1
  • 1
Sri Hari Vignesh
  • 256
  • 4
  • 11

1 Answers1

1

This is a matter of parsing a request string into an appropriate dictionary representation. To do so, you can use the parse_qs function.

In Python 2,

from urlparse import parse_qs

var = parse_qs(payload_from_POST)
print var['comment'][0]

In Python 3

from urllib.parse import parse_qs

var = parse_qs(payload_from_POST)
print(var['comment'][0])


An important note: parse_qs will return a JSON that maps keys to lists, NOT strings. So in order to actually print "The Quick Brown Fox Jumped Over The Lazy Dog", you have to access var['comment'][0], because var['comment'] would just return you a list with one element in it.

Nenya Edjah
  • 11
  • 1
  • 2