0

I'm trying to convert an string into json output from local Data or Those datas from BeautifulSoup output as Json.for example:

#! /usr/bin/python

data = ('Hello')
print data

and i need to convert this Hello as json output.

How can do that?

is this possible?

2 Answers 2

1

Check out the json module in Python https://docs.python.org/2/library/json.html

import json
json.dumps({"hello": 0}, sort_keys=True)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the json module to encode Python objects as JSON, e.g.

>>> import json
>>> data = ('Hello')
>>> json.dumps(data)
'"Hello"'

>>> data = ('Hello', 'There')
>>> json.dumps(data)
'["Hello", "There"]'


>>> data = {'message': 'Hello'}
>>> json.dumps(data)
'{"message": "Hello"}'

6 Comments

thanks.but it seems it doesnt work on these codes: paste.ubuntu.com/12134971
When I run the linked code it outputs this JSON encoded string: '"\\u062a\\u0644\\u06af\\u0631\\u0627\\u0645 \\u0628\\u0631\\u0627\\u06cc \\u0633\\u06cc\\u0645\\u0628\\u06cc\\u0646 \\u0648 \\u062c\\u0627\\u0648\\u0627 !\\u0627\\u0635\\u0627 \\u062f\\u0627\\u0631\\u06cc\\u0645 \\u061f\\u061f\\u061f"'. It is only displaying the last one in the title_rows list, but it does appear to work.
yeah, lets try it with english words.
What are you really trying to do? Do you want a JSON encoded list of URLs? If you are trying to extract the URL, try json.dumps(texts.a['href']), e.g.'"showthread.php?148784-\\u062a\\u0644\\u06af\\u0631\\u0627\\u0645-\\u0628\\u0631\\u0627\\u06cc-\\u0633\\u06cc\\u0645\\u0628\\u06cc\\u0646-\\u0648-\\u062c\\u0627\\u0648\\u0627-!\\u0627\\u0635\\u0627-\\u062f\\u0627\\u0631\\u06cc\\u0645-\\u061f\\u061f\\u061f&s=a3e13225636cf0a310f89089f1b61161&goto=newpost"'
actually i was trying to achieve this persian words into json, but it seems its returns last one as json:-?
|

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.