Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting JSON to string in Python

I did not explain my questions clearly at beginning. Try to use str() and json.dumps() when converting JSON to string in python.

>>> data = {'jsonKey': 'jsonValue',"title": "hello world"} >>> print json.dumps(data) {"jsonKey": "jsonValue", "title": "hello world"} >>> print str(data) {'jsonKey': 'jsonValue', 'title': 'hello world'} >>> json.dumps(data) '{"jsonKey": "jsonValue", "title": "hello world"}' >>> str(data) "{'jsonKey': 'jsonValue', 'title': 'hello world'}" 

My question is:

>>> data = {'jsonKey': 'jsonValue',"title": "hello world'"} >>> str(data) '{\'jsonKey\': \'jsonValue\', \'title\': "hello world\'"}' >>> json.dumps(data) '{"jsonKey": "jsonValue", "title": "hello world\'"}' >>>  

My expected output: "{'jsonKey': 'jsonValue','title': 'hello world''}"

>>> data = {'jsonKey': 'jsonValue',"title": "hello world""}   File "<stdin>", line 1     data = {'jsonKey': 'jsonValue',"title": "hello world""}                                                           ^ SyntaxError: EOL while scanning string literal >>> data = {'jsonKey': 'jsonValue',"title": "hello world\""} >>> json.dumps(data) '{"jsonKey": "jsonValue", "title": "hello world\\""}' >>> str(data) '{\'jsonKey\': \'jsonValue\', \'title\': \'hello world"\'}' 

My expected output: "{'jsonKey': 'jsonValue','title': 'hello world\"'}"

It is not necessary to change the output string to json (dict) again for me.

How to do this?

like image 573
BAE Avatar asked Jan 04 '16 21:01

BAE


People also ask

How do I convert a JSON object to a string in Python?

Try to use str() and json. dumps() when converting JSON to string in python. It is not necessary to change the output string to json (dict) again for me.

How do I convert a JSON to a string?

Use 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.

Can I convert JSON to text?

Users can also Convert JSON File to Text by uploading the file. Download converted file with txt extension.


1 Answers

json.dumps() is much more than just making a string out of a Python object, it would always produce a valid JSON string (assuming everything inside the object is serializable) following the Type Conversion Table.

For instance, if one of the values is None, the str() would produce an invalid JSON which cannot be loaded:

>>> data = {'jsonKey': None} >>> str(data) "{'jsonKey': None}" >>> json.loads(str(data)) Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads     return _default_decoder.decode(s)   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode     obj, end = self.raw_decode(s, idx=_w(s, 0).end())   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode     obj, end = self.scan_once(s, idx) ValueError: Expecting property name: line 1 column 2 (char 1) 

But the dumps() would convert None into null making a valid JSON string that can be loaded:

>>> import json >>> data = {'jsonKey': None} >>> json.dumps(data) '{"jsonKey": null}' >>> json.loads(json.dumps(data)) {u'jsonKey': None} 
like image 127
alecxe Avatar answered Sep 27 '22 20:09

alecxe