Skip to content Skip to sidebar Skip to footer

Flask Request.json Order

I get JSON data submitted to my api (built with Flask) endpoint and I need that data to be exported to excel but the problem is that once my api receives the data and loads it with

Solution 1:

Python dictionaries are unordered, as are JSON objects. The behaviour you see is normal.

If you need to retain ordering, don't use a dict() but a list up tuples instead:

   [
       ["date", "2013-01-08"],
       ["group.groupname", "customerService"],
       ["user.NameSurname", "Romāns Tiščenko"],
       ["forwarding_number", "66055002"],
       ["reciver_number", "66055002"],
       ["CallerNameSurname", false],
       ["alert", "00:00:14"],
       ["connection", "00:00:53"],
       ["call_summ", "00:01:07"]
   ],
   # ... etc.

Or you could also use the data['header'][0] string to read the body dict() values in order, since that is already ordered for you:

rows= data['body']
i =1forrowinrows:
    x = sheet.row(i)
    y =0for col in headers:
        x.write(y, row.get(col, ''))
        y +=1
    i +=1

Post a Comment for "Flask Request.json Order"