0

I'm new to Python, been learning it at college and I have a question. First of all, this is the important part of the code in question:

#This is on my TCP client
    name = raw_input("Box name: ")
    timestamp = time.time()

    json_dict = {'type': 'CREATE',
                 'name': name,
                 'timestamp': timestamp}

    print json_dict
    tcp_s.send(pickle.dumps(json_dict))

#This is on my TCP server
    json_dict = pickle.loads(client_s.recv(65536))
    file_received = json.dumps(json_dict,indent=4)
    print file_received

Im basically trying to carry over information from the client to the server and have it convert into JSON, which is successful. My issue: the order in which it presents the type, name and timestamp tags arent as I want them to be. I think this might be due to using dictionaries which arent ordered and that is why Im here.

The current output:

{
    "timestamp": 1492173052.087644, 
    "type": "CREATE", 
    "name": "test"
}

Any suggestion to have the output be like this?

{
    "type": "CREATE", 
    "name": "test"    
    "timestamp": 1492173052.087644,   
}
  • You've posted a valid data structure, but that often isn't what you want on a printed output. You're correct that the order is influenced by dict behaviour but if you want the values printed in a certain order, do you really want the enclosing curly braces too? If not, just use keys as you would a dictionary to print the values in the order you want. – roganjosh Apr 14 '17 at 12:44
  • The order doesn't matter. – Chris Apr 14 '17 at 12:44
  • JSON objects, like Python dictionaries, are *unordered structures*. Why do you need to enforce a specific order? – Martijn Pieters Apr 14 '17 at 12:46
  • Yes @roganjosh Id want the curly braces aswell. I wouldnt normally mind the ordering matter, however this is for a project that will actually require the correct ordering in the future. –  Apr 14 '17 at 12:46
  • @MartijnPieters Its a specification from the project itself. –  Apr 14 '17 at 12:47
  • does the specification specify that the order is significant or did it just happen to list the keys in that order? – Martijn Pieters Apr 14 '17 at 12:48
  • It's an unrealistic specification. Nobody expects order in that JSON structure. Accessing/printing in a specific order is fine, but I can't think of any purpose served by trying to actually order the JSON unless you have a list. – roganjosh Apr 14 '17 at 12:48
  • 1
    I agree it's not a realistic expectation, as the order of JSON is more or less irrelevant. However as a programming exercise I can see how they might ask for it. This should help. http://stackoverflow.com/questions/6921699/can-i-get-json-to-load-into-an-ordereddict-in-python – Chris Apr 14 '17 at 12:48
  • @Chris learn something new every couple of minutes :) – roganjosh Apr 14 '17 at 12:51
  • Thanks everyone for the help, It is functioning like it should now ^^ And yes, the exercise itself asked for this specific order to be presented. –  Apr 14 '17 at 12:56

0 Answers0