0

I'm stuck on a simple issue:

I get through urllib a JSON app list which looks like this :

    "completedapps" : [ {
    "starttime" : 1520863179923,
    "id" : "app-20180312145939-0183",
    "name" : "IE_Traitement_3",
    "cores" : 1,
    "user" : "root",
    "memoryperslave" : 1024,
    "submitdate" : "Mon Mar 12 14:59:39 CET 2018",
    "state" : "FINISHED",
    "duration" : 212967
  }, {
    "starttime" : 1520863398147,
    "id" : "app-20180312150318-0186",
    "name" : "IE_Traitement_3",
    "cores" : 1,
    "user" : "root",
    "memoryperslave" : 1024,
    "submitdate" : "Mon Mar 12 15:03:18 CET 2018",
    "state" : "FINISHED",
    "duration" : 6321
  }, {
    "starttime" : 1520863387941,
    "id" : "app-20180312150307-0185",
    "name" : "IE_Traitement_0A",
    "cores" : 1,
    "user" : "root",
    "memoryperslave" : 1024,
    "submitdate" : "Mon Mar 12 15:03:07 CET 2018",
    "state" : "FINISHED",
    "duration" : 149536
  }, { ... }]

I would like to get the most recent element for app named "IE_Traitement_OA", so I begin with filtering my JSON like this :

[app for app in parsedjson['completedapps'] if app['name'] == "IE_Traitement_OA"]

But I'm stuck now, I have no idea about how could I get the most recent "app" ? I think I have to use the starttime or submitdate field but I don't know how to deal with that. Could you help me?

nj2237
  • 1,220
  • 3
  • 21
  • 25
Kementari
  • 23
  • 1
  • 5
  • 3
    JSON data till be converted to a dictionary structure. Dictionaries are inherently not a ordered structure. You need to convert these using a OrderedDict if you want to preserve the order. – Torxed Mar 16 '18 at 10:03
  • Okay I've updated my json.load method to add parameter "object_pairs_hook=OrderedDict", but next ? – Kementari Mar 16 '18 at 10:20

3 Answers3

2

you can filter using the following:

a = list(filter(lambda x: x['name'] == 'IE_Traitement_0A', data['completedapps']))

a will contain a list of all dict that match your filter and then you can sort the the list for the latest one -- using whatever key to sort it by

sorted_a = sorted(a, key=lambda k: k['starttime'])

if you want only one then select the first element of sorted_a assuming it's not empty.

EDIT: use min instead of sorted thanks for the tip @VPfB

min_a = min(a, key=lambda k: k['starttime'])
gyx-hh
  • 1,421
  • 1
  • 10
  • 15
  • If you want only one element, use `min` instead of sorting the whole sequence and then throwing away everything but the first item. – VPfB Mar 16 '18 at 11:16
  • thanks @VPfB this does work better -- i have edited my answer – gyx-hh Mar 16 '18 at 11:27
0

If you will use starttime you can use the max function like that:

data = [{
    "starttime" : 1520863398147,
    "id" : "app-20180312150318-0186",
    "name" : "IE_Traitement_3",
    "cores" : 1,
    "user" : "root",
    "memoryperslave" : 1024,
    "submitdate" : "Mon Mar 12 15:03:18 CET 2018",
    "state" : "FINISHED",
    "duration" : 6321
}, {
    "starttime" : 1520863387941,
    "id" : "app-20180312150307-0185",
    "name" : "IE_Traitement_0A",
    "cores" : 1,
    "user" : "root",
    "memoryperslave" : 1024,
    "submitdate" : "Mon Mar 12 15:03:07 CET 2018",
    "state" : "FINISHED",
    "duration" : 149536
}]

most_recent = max(data,key=lambda e: e['starttime'])
print(most_recent)

Now, if you want use the submitdate you need to convert first

At this link there some examples of conversion: Converting string into datetime

Good look!

0
req_json = """{"completedapps" : [ {
    "starttime" : 1520863179923,
    "id" : "app-20180312145939-0183",
    "name" : "IE_Traitement_3",
    "cores" : 1,
    "user" : "root",
    "memoryperslave" : 1024,
    "submitdate" : "Mon Mar 12 14:59:39 CET 2018",
    "state" : "FINISHED",
    "duration" : 212967
  }, {
    "starttime" : 1520863398147,
    "id" : "app-20180312150318-0186",
    "name" : "IE_Traitement_3",
    "cores" : 1,
    "user" : "root",
    "memoryperslave" : 1024,
    "submitdate" : "Mon Mar 12 15:03:18 CET 2018",
    "state" : "FINISHED",
    "duration" : 6321
  }, {
    "starttime" : 1520863387941,
    "id" : "app-20180312150307-0185",
    "name" : "IE_Traitement_0A",
    "cores" : 1,
    "user" : "root",
    "memoryperslave" : 1024,
    "submitdate" : "Mon Mar 12 15:03:07 CET 2018",
    "state" : "FINISHED",
    "duration" : 149536
  } ]}"""

import json
data = json.loads(req_json)

print(sorted(data['completedapps'], key=lambda x: x['starttime'])[0]['id'])

out:

app-20180312145939-0183

Explanation: first get list of dict and sort then by timestamp.

Rahul
  • 10,830
  • 4
  • 53
  • 88