Correct way of sending JSON Data with encoding in Python
<p>I am developing API's on Django, and I am facing a lot of issues in encoding the data in python at the back-end and decoding it at front-end on java. </p>

<p>Any standard rules for sending correct JSON Data to client application efficiently? </p>

<p>There are some Hindi Characters which are not received properly on front end, it gives error saying that "JSON unterminated object at character" So I guess the problem is on my side</p>

<p><code>json.loads</code> and <code>json.dumps</code> are generally used to encode and decode JSON data in python.  </p>

<p><code>dumps</code> takes an object and produces a string and <code>load</code> would take a file-like object, read the data from that object, and use that string to create an object.  </p>

<p>The encoder understands Pythonâs native types by default (string, unicode, int, float, list, tuple, dict).</p>

<pre><code>import json

data = [ { 'a':'A', 'b':(2, 4), 'c':3.0 } ]
print 'DATA:', repr(data)

data_string = json.dumps(data)
print 'JSON:', data_string
</code></pre>

<p>Values are encoded in a manner very similar to Pythonâs repr() output.</p>

<pre><code>$ python json_simple_types.py

DATA: [{'a': 'A', 'c': 3.0, 'b': (2, 4)}]
JSON: [{"a": "A", "c": 3.0, "b": [2, 4]}]
</code></pre>

<p>Encoding, then re-decoding may not give exactly the same type of object.</p>

<pre><code>import json

data = [ { 'a':'A', 'b':(2, 4), 'c':3.0 } ]
data_string = json.dumps(data)
print 'ENCODED:', data_string

decoded = json.loads(data_string)
print 'DECODED:', decoded

print 'ORIGINAL:', type(data[0]['b'])
print 'DECODED :', type(decoded[0]['b'])
</code></pre>

<p>In particular, strings are converted to unicode and tuples become lists.</p>

<pre><code>$ python json_simple_types_decode.py

ENCODED: [{"a": "A", "c": 3.0, "b": [2, 4]}]
DECODED: [{u'a': u'A', u'c': 3.0, u'b': [2, 4]}]
ORIGINAL: &lt;type 'tuple'&gt;
DECODED : &lt;type 'list'&gt;
</code></pre>

python json django