'How to Return String Content inside JSON objects Returned by json.loads() Function, insdead of Unicode Objects, in Python 2.7?

In want to convert a string to a JSON object in Python 2.7. The example code follows:

import json

string1 = '{ "model": "UR32", "sn": "6218B1022170" }'

res = json.loads(string1)

print(res)
print(res['model'])
print(type(res['model']))

The results in Python 2.7 for above code is as follows:

{u'model': u'UR32', u'sn': u'6218B1022170'}
UR32
<type 'unicode'>

In Python 2.7, when the json.loads() is done, the contents of the object are converted to unicode objects, instead of string objects.

But,in Python 3, I get following results:

{'model': 'UR32', 'sn': '6218B1022170'}
UR32
<class 'str'>

Please see. It has resulted string objects. That is what I need. I cannot use Python 3. I am coding a cellular router. Its Python SDK is only available for Python 2.7. How can I get an string content inside my JSON object?

I referred to Python 2 documentation. It says as follows, which I cannot figure out what to do:

json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])

Deserialize s (a str or unicode instance containing a JSON document) to a Python object using this conversion table.

If s is a str instance and is encoded with an ASCII based encoding other than UTF-8 (e.g. latin-1), then an appropriate encoding name must be specified. Encodings that are not ASCII based (such as UCS-2) are not allowed and should be decoded to unicode first.

Please help me to find out how to avoid conversion of the JSON content to unicode objects and how to get string objects inside the returned JSON object by json.loads() function in Python 2.7.



Solution 1:[1]

If you are working with Python 2.7, the best thing is to continue with unicode content inside JSON. It will not affect you in any way.

If you need a neat string to send outside, just use json.dumps() at anytime when you need to send the JSON object outside the Python 2 program.

Example 1

import json

string1 = '{ "model": "UR32", "sn": "6218B1022170" }'

res = json.loads(string1)

print(res)

res2 = json.dumps(res)

print(res2)
print(type(res2))

Output:

{u'model': u'UR32', u'sn': u'6218B1022170'}
{"model": "UR32", "sn": "6218B1022170"}
<type 'str'>

Example 2

import json

wan = '''{
    "status": 1,
    "cur_link": 1,
    "ip": "192.168.254.20/24",
    "time": "5 days, 02:05:04",
    "wan_exist": 1,
    "mac": "24:e1:24:f1:6d:3c",
    "ipv6": "fe80::26e1:24ff:fef1:6d3a/64"
  }'''

lan = '''{"ip": "192.168.1.1/24",
    "connected": 0,
    "ipv6": "fe80::c863:54ff:fe37:dc45/64"
  }'''
  
wan_json = json.loads(wan)
lan_json = json.loads(lan)

final_json = {
    'wan': wan_json,
    'lan': lan_json
}

print(final_json)

final_string = json.dumps(final_json)
print(final_string)
print(type(final_string))

Output:

{'wan': {u'status': 1, u'cur_link': 1, u'ip': u'192.168.254.20/24', u'time': u'5 days, 02:05:04', u'ipv6': u'fe80::26e1:24ff:fef1:6d3a/64', u'mac': u'24:e1:24:f1:6d:3c', u'wan_exist': 1}, 'lan': {u'ip': u'192.168.1.1/24', u'connected': 0, u'ipv6': u'fe80::c863:54ff:fe37:dc45/64'}}
{"wan": {"status": 1, "cur_link": 1, "ip": "192.168.254.20/24", "time": "5 days, 02:05:04", "ipv6": "fe80::26e1:24ff:fef1:6d3a/64", "mac": "24:e1:24:f1:6d:3c", "wan_exist": 1}, "lan": {"ip": "192.168.1.1/24", "connected": 0, "ipv6": "fe80::c863:54ff:fe37:dc45/64"}}
<type 'str'>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Janith Bandara