0

I am retrieving JSON data with the following format:

{
    "first": "Data1",
    "second": Data2,
    "third": "Data3",
    "fourth": "Data4",
    "Some random ID": "Data5"
}

I am trying to find the last left hand argument in this JSON, so "Some random ID". I do not know the random ID, so I am just trying to get the last Dict in the JSON's left hand argument. How do I obtain "Some random ID"?

Greg
  • 168
  • 1
  • 3
  • 19
  • refer to this link: https://stackoverflow.com/questions/4859292/how-to-get-a-random-value-from-dictionary-in-python for your answer – Jaya Shankar B Aug 09 '20 at 17:52
  • So, Python dict objects are only ordered in Python 3.6+ (really, only as. a language guarantee in 3.7+). In any case, you can just do `list(my_dict)[-1]` to get the last key. – juanpa.arrivillaga Aug 09 '20 at 17:57

1 Answers1

0
print(list(dict)[-1]) #last key
print(dict[list(dict)[-1]]) #last key's value

OR

keys = list(dict.keys())
keys.reverse()
print(keys[0]) #last key
print(dict[keys[0]]) #last key's value
Just for fun
  • 4,102
  • 1
  • 5
  • 11