Kementari
Kementari

Reputation: 23

How to get last element of a JSON list

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?

Upvotes: 0

Views: 3130

Answers (3)

gyx-hh
gyx-hh

Reputation: 1431

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'])

Upvotes: 2

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!

Upvotes: 0

Rahul
Rahul

Reputation: 11530

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.

Upvotes: 0

Related Questions