Z
Z
Zef1rok2021-07-16 08:38:40
Python
Zef1rok, 2021-07-16 08:38:40

How to get the largest value and the name attached to it from json?

{
  "858585885856985": 8.2,
  "841224131321321": 100.6
}

there is such a json, how to get the largest value from it and the attached name to it?
but this json will have a little more values

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2021-07-16
@Zef1rok

import operator
# import json
# a = json.loads(...)  если json  в виде строки прилетает
a = {
   "858585885856985": 8.2,
   "841224131321321": 100.6
}
print(max(a.items(), key=operator.itemgetter(1)))
# ('841224131321321', 100.6)

J
Jalal Nasirov, 2021-07-16
@Best_Loops

json = {
  "858585885856985": 8.2,
  "841224131321321": 100.6,
  "937268282628292": 677.1,
  "815158272528286": 87.5
}

num = max(json)

json['name'] = json[num]

del json[num]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question