prasunnair
prasunnair

Reputation: 92

Python json.dumps object when used in String context has length restrictions

I have the following condition. A dict "result" passed to json.dumps as follows. The dict 'result', and output are printed out as shown below.

dumpclean(result)
jsonString = json.dumps(result)
dumpclean(jsonString)
print "Value being sent to server is %s" % jsonString

Here dumpclean implementation is as follows

def dumpclean(obj):
    if type(obj) == dict:
        for k, v in obj.items():
            if hasattr(v, '__iter__'):
                print k
                dumpclean(v)
            else:
                print '%s : %s' % (k, v)
    elif type(obj) == list:
        for v in obj:
            if hasattr(v, '__iter__'):
                dumpclean(v)
            else:
                print v
    else:
        print obj

The output I see in log is something strange

2015-07-04 10:47:06 route_id : DEMPUN0001027
2015-07-04 10:47:06 version : 1
2015-07-04 10:47:06 frequency : 1
2015-07-04 10:47:06 end_time : 08:00
2015-07-04 10:47:06 type : schoolbuspickup
2015-07-04 10:47:06 points
2015-07-04 10:47:06 Latitude : 18.5123307247
2015-07-04 10:47:06 display_name : Check Point 0
2015-07-04 10:47:06 id : DEMPUN0001027000
2015-07-04 10:47:06 Longitude : 73.7916469574
2015-07-04 10:47:06 Latitude : 18.5112523117
2015-07-04 10:47:06 display_name : Check Point 1
2015-07-04 10:47:06 id : DEMPUN0001027001
2015-07-04 10:47:06 Longitude : 73.7930846214
2015-07-04 10:47:06 Latitude : 18.5095634624
2015-07-04 10:47:06 display_name : Check Point 2
2015-07-04 10:47:06 id : DEMPUN0001027002
2015-07-04 10:47:06 Longitude : 73.7935996056
2015-07-04 10:47:06 Latitude : 18.5081798145
2015-07-04 10:47:06 display_name : Check Point 3
2015-07-04 10:47:06 id : DEMPUN0001027003
2015-07-04 10:47:06 Longitude : 73.7945008278
2015-07-04 10:47:06 Latitude : 18.5073658988
2015-07-04 10:47:06 display_name : Check Point 4
2015-07-04 10:47:06 id : DEMPUN0001027004
2015-07-04 10:47:06 Longitude : 73.7984704971
2015-07-04 10:47:06 Latitude : 18.5067554594
2015-07-04 10:47:06 display_name : Check Point 5
2015-07-04 10:47:06 id : DEMPUN0001027005
2015-07-04 10:47:06 Longitude : 73.8006162643

2015-07-04 10:47:06 {"display_name": "Trial1", "start_time": "07:00", "days_valid": ["Mon", "Tue", "Wed", "Thu", "Fri"], "route_id": "DEMPUN0001027", "version": 1, "frequency": 1, "end_time": "08:00", "type": "schoolbuspickup", "points": [{"Latitude": 18.5123307247275, "display_name": "Check Point 0", "id": "DEMPUN0001027000", "Longitude": 73.79164695739746}, {"Latitude": 18.511252311658, "display_name": "Check Point 1", "id": "DEMPUN0001027001", "Longitude": 73.79308462142944}, {"Latitude": 18.5095634624415, "display_name": "Check Point 2", "id": "DEMPUN0001027002", "Longitude": 73.7935996055603}, {"Latitude": 18.5081798145188, "display_name": "Check Point 3", "id": "DEMPUN0001027003", "Longitude": 73.7945008277893}, {"Latitude": 18.5073658987508, "display_name": "Check Point 4", "id": "DEMPUN0001027004", "Longitude": 73.79847049713135}, {"Latitude": 18.506755459385, "display_name": "Check Point 5", "id": "DEMPUN0001027005", "Longitude": 73.80061626

2015-07-04 10:47:06 Value being sent to server is {"display_name": "Trial1", "start_time": "07:00", "days_valid": ["Mon", "Tue", "Wed", "Thu", "Fri"], "route_id": "DEMPUN0001027", "version": 1, "frequency": 1, "end_time": "08:00", "type": "schoolbuspickup", "points": [{"Latitude": 18.5123307247275, "display_name": "Check Point 0", "id": "DEMPUN0001027000", "Longitude": 73.79164695739746}, {"Latitude": 18.511252311658, "display_name": "Check Point 1", "id": "DEMPUN0001027001", "Longitude": 73.79308462142944}, {"Latitude": 18.5095634624415, "display_name": "Check Point 2", "id": "DEMPUN0001027002", "Longitude": 73.7935996055603}, {"Latitude": 18.5081798145188, "display_name": "Check Point 3", "id": "DEMPUN0001027003", "Longitude": 73.7945008277893}, {"Latitude": 18.5073658987508, "display_name": "Check Point 4", "id": "DEMPUN0001027004", "Longitude": 73.79847049713135}, {"Latitude": 18.506755459385, "display_name": "Check Point 5", "id": "DEMPUN0001027

It seems as the output of json.dumps when used in a string context is getting truncated. So is there a length of string restriction for the print function ?

The main issue is the jsonString so generated when used to in a 'POST' is also resulting in a truncated JSON being posted.

req = Request("http://xyz/api/v1/Route/" + routeCode, jsonString)
response = urlopen(req)

SOLUTION:

This is related to the field size restriction of the cache implementation used to store JSON on receiving server. It was not big enough to cover for larger JSON data.

Upvotes: 0

Views: 4642

Answers (1)

Marcus Müller
Marcus Müller

Reputation: 36337

So is there a length of string restriction for the print function ?

No. Python just passes on the string to the output.

Your logger, however, will very very likely have such a per-line limit to keep programs from making the log file size explode.

Upvotes: 1

Related Questions