You can check if the key exists (you can also check if the length is correct):

if len(json_stub) > 0 and json_stub[-1].get('Value1') is not None:
    value1_node = json_stub-1
else:
    # 'Value1' key does not exist  
Answer from Surcle on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › reading the last element in a json file
r/learnpython on Reddit: Reading the last element in a json file
March 30, 2019 -

I have a json file that looks like this: https://gist.github.com/SirDarknight/95cf48d46c17059a5f42a38ec70c7fa5

What's the best way to get the last object's "Title" key? (In this case: Mercy Black (2019) )

Edit: I'm thinking about wrapping the whole thing in a list and then loading the list

Discussions

Get the Last Element in A Large JSON file using Python - Stack Overflow
I want to get the last element in a JSON file This file is very large, so I do not want to load it into memory (using json.loads) Happy to use something like ijson or jsonparser, but can't figure o... More on stackoverflow.com
🌐 stackoverflow.com
python - How to get last element of a JSON list - Stack Overflow
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", "nam... More on stackoverflow.com
🌐 stackoverflow.com
Get last item in JSON array with Python - Stack Overflow
I'm trying to grab the last item in a json array and haven't been able to come across any documentation that shows how to do this. {'numbers': [1, 2, 3]} how do I just get 3? ANSWERED: import json More on stackoverflow.com
🌐 stackoverflow.com
Get the last element in json array
how can I get the last element in the json array in the seats object. I want to get the countryid of with the value of 845, however this json is dynamic so i want to get the last element in the seats More on stackoverflow.com
🌐 stackoverflow.com
December 24, 2020
🌐
Stack Overflow
stackoverflow.com › questions › 65447666 › get-the-last-element-in-a-large-json-file-using-python
Get the Last Element in A Large JSON file using Python - Stack Overflow
import ijson with open('customerAccounts.json', 'rb') as file: for key, value in ijson.kvitems(file, ''): pass print(key, value) key and value are here the last values from the iteration process. There are other ways to get the latest value from an iteration that might be faster (e.g., using a deque of size 1), but this should do.
Top answer
1 of 3
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'])
2 of 3
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.

🌐
Stack Overflow
stackoverflow.com › questions › 77223566 › get-last-item-in-json-array-with-python
Get last item in JSON array with Python - Stack Overflow
Copyimport json array = json.load(str({'numbers': [1, 2, 3]})) lastItem = array['numbers'][-1] # This is the format. ... semi-related in that it shows examples of numpy multi-dimensional array vs python list access stackoverflow.com/questions/18272160/…
🌐
Microsoft Power Platform Community
powerusers.microsoft.com › t5 › Building-Flows › Parsing-JSON-get-the-last-array › td-p › 1270092
Forums | Microsoft Power Platform Community
September 14, 2021 - Quickly search for answers, join discussions, post questions, and work smarter in your business applications by joining the Microsoft Dynamics 365 Community.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 68394053 › how-do-i-get-the-last-element-of-a-json-list
python - How do I get the last element of a json list? - Stack Overflow
July 15, 2021 - How do I get the last element of a list? [closed] (20 answers) Closed 4 years ago. Here I'm trying to get the last m2id to print it into console. [{ "m2id":"865199733949071370", "author":{ "id":"862323352869797948" } }, { "m2id":"865199658103078925", "author":{ "id":"751742911682445312" } }] And here's the code I wrote for it. (r.json being the json above): data = r.json() for id in data: id3 = id["id"][-1] print(id3) Running the code gave me the last number of the first m2id, Which is not what I want.
🌐
Stack Overflow
stackoverflow.com › questions › 49505335 › how-to-get-the-last-value-from-the-list-using-python
json - How to get the last value from the list using python? - Stack Overflow
I will get the result ['{"osc":{"version":"1.0"}}'] from my script and from this ['{"osc":{"version":"1.0"}}'] i should get "1.0" value and sometimes the data will be ['{"device":{"network":{"ipv4_dante":{"auto":"1.0"}}}}'] . In any condition i need to get "1.0" value if data is varying also. 2018-03-27T07:35:43.873Z+00:00 ... def bottom_value(in_data): def recurse_to_bottom(a_dict): if isinstance(a_dict, dict): a_key = list(a_dict)[0] return recurse_to_bottom(a_dict[a_key]) return a_dict return recurse_to_bottom(json.loads(in_data['reply']))
🌐
Cloudera Community
community.cloudera.com › t5 › Support-Questions › find-last-element-of-json-in-nifi › m-p › 322690
Solved: find last element of json in nifi - Cloudera Community - 322690
August 19, 2021 - [ { "operation": "modify-overw... ,H_GROSS_AMNT will still hold the string comma separated and u can use H_GROSS_AMNT:substringAfterLast(,) which extracts last element from the string value ....
🌐
SitePoint
sitepoint.com › javascript
How can I access last object in JSON array - JavaScript
February 2, 2015 - I have an object in localstorage and would like to match the last element that I have added when I called $(‘form’).serializeArray(); I am trying to match the text from a dropdown that is populated with the keys from lo…
🌐
Gatling
community.gatling.io › gatling (open-source)
How to extract the last array element in jsonpath with findAll - Gatling (Open-Source) - Gatling
June 24, 2022 - How to do it Example: In the below ... want to choose the last value of the array always. sample JSON { "data": { "A": { "A1": { "colour": "1", }, "main": [ { ......
🌐
Gatling
community.gatling.io › gatling (open-source)
Get last element of JSON tab of objects response - Gatling (Open-Source) - Gatling
June 28, 2023 - I have a project where I need to get the last object of a JSON list that is returned in JSON but I can’t get any idea of how to from a .check My builder: ChainBuilder getAllOrders = exec(http("Orders: /api/orders") .get("/api/orders") .header("Authorization", "Bearer ${BearerToken}") .check(status().is(200)) .check(jsonPath("$..id").saveAs("OrderIds"))); The response of the get is "[ { "id": 3, "order_number": "{ord_nb}", "dat...
🌐
GitHub
github.com › microsoft › AL › issues › 6323
Get last element in Json Array with JSON Path · Issue #6323 · microsoft/AL
November 26, 2020 - If I use QueryString := '$.Datensätze[?(@.Datum==''2020-11-25T00:00:00'')].Überzeit'; I get the desired result "3202". But I do not know the date of the last element beforehand, that's why I must be able just to select the last element in the array: I tested the syntax in the JSON Path Online Evaluator and it worked.
Author: microsoft
🌐
Temboo
temboo.com › python › parsing-json
Parsing JSON in Python
6 Next, we parsed out the piece of data we want from the JSON. It helps to look at the JSON file's structure to get an idea of how it is organized. The two main elements you should look for are: