python- reading JSON in ascending order -


i'm trying parse json file looks this:

{ "my_test":[     { "group_name":"group-a", "results": [         { "test_name": "test1", "time": "8.556", "status": "pass" },         { "test_name": "test2", "time": "45.909", "status": "pass" },         { "test_name": "test3", "time": "9.383", "status": "fail" },         ...     } } 

how can print out test results in ascending order? (by name or time)

edit: output in ascending order of time:

test1 8.556 test3 9.383 test2 45.909 

you try this, using builtin sorted function.

from json import loads  json_data = """{     "my_test": [{         "group_name": "group-a",         "results": [{             "test_name": "test1",             "time": "8.556",             "status": "pass"         }, {             "test_name": "test2",             "time": "45.909",             "status": "pass"         }, {             "test_name": "test3",             "time": "9.383",             "status": "fail"         }]     }] }"""  data = loads(json_data)  group in data["my_test"]:     print group["group_name"]     sorted_results = sorted(group["results"], key=lambda a: float(a["time"]))     result in sorted_results:         print(result["test_name"] + ": " + result["time"]) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -