Home > Software design >  How to get JSON data between specific date ranges in Python?
How to get JSON data between specific date ranges in Python?

Time:09-04

I am new to Python and I am facing some difficulties. I have this JSON data set consisting of meteorite landings and I want to find the meteorite landing with the highest mass in the last 10 years.

JSON sample:

[
{
"name": "Aachen",
"id": "1",
"nametype": "Valid",
"recclass": "L5",
"mass": "21",
"fall": "Fell",
"year": "1880-01-01T00:00:00.000",
"reclat": "50.775000",
"reclong": "6.083330",
"geolocation": {
"type": "Point",
"coordinates": [
6.08333,
50.775
]
}
},
{
"name": "Aarhus",
"id": "2",
"nametype": "Valid",
"recclass": "H6",
"mass": "720",
"fall": "Fell",
"year": "1951-01-01T00:00:00.000",
"reclat": "56.183330",
"reclong": "10.233330",
"geolocation": {
"type": "Point",
"coordinates": [
10.23333,
56.18333
]
}
},

My code is th following, I have got to the point to get the date range of last 10 years but not able to link the mass within the date range.

import json, datetime
from dateutil.relativedelta import relativedelta
f = open('nasadata.json')
data = json.load(f)
start_date = datetime.datetime.now() - relativedelta(years = 10)
end_date = datetime.datetime.now()
for item in data:
    year = item.get('year')
    if year is not None:
        year = datetime.datetime.strptime(year, "%Y-%m-%dT%H:%M:%S.%f")
    name = item.get('name')
    mass = item.get('mass')
           
    if year is not None and year >= start_date and year <= end_date:
        max_mass = max(mass)
        print(max_mass)

How do I iterate through JSON to get the maximum mass meteorite name and mass values within the date range of 10 years ?

CodePudding user response:

I would make a slight optimisation by maintaining dates in the "%Y-%m-%dT%H:%M:%S.%f" format as then they can be sorted alphabetically. Also you don't need to compare against now as presumably there are no future meteorite falls recorded? So then your code can be:

start_date = datetime.datetime.now() - relativedelta(years = 10)
start_date = start_date.strftime("%Y-%m-%dT%H:%M:%S.%f")

recent = [m for m in data if m.get('year', '0') >= start_date]
heaviest = sorted(recent, key=lambda m:float(m.get('mass', 0)), reverse=True)[0]

print(heaviest)
  • Related