我有以下JSON字典。
我想做的是删除“ orbiting_body”不是“ Earth”的所有“ close_approach_data”对象。
问题是orbiting_body可能有多个对象:“ Earth”,而在所有这些对象之间,我试图使该对象的最小“ approach_date”最小。

data = [
  {
    "id": "01",
    "close_approach_data": [
      {
        "orbiting_body": "Earth",
        "approach_date": "1945-06-07"
      },
      {
        "orbiting_body": "Earth",
        "approach_date": "1975-06-07"
      },
      {
        "orbiting_body": "Mars",
        "approach_date": "1935-06-07"
      }
    ]
  },
{
    "id": "02",
    "close_approach_data": [
      {
        "orbiting_body": "Earth",
        "approach_date": "1945-06-07"
      },
      {
        "orbiting_body": "Earth",
        "approach_date": "1975-06-07"
      },
      {
        "orbiting_body": "Mars",
        "approach_date": "1935-06-07"
      }
    ]
  }
]


我想得到这个:

data = [
  {
    "id": "01",
    "close_approach_data": {
      "orbiting_body": "Mars",
      "approach_date": "1935-06-07"
    }
  },
  {
    "id": "02",
    "close_approach_data": {
      "orbiting_body": "Mars",
      "approach_date": "1935-06-07"
    }
  }
]


所以我想提出一些代码:

earthObjs =[]
for element in data:
    for subel in element["close_approach_data"]:
        if ([subel][0]["orbiting_body"]=="Earth"):
            #then i would have to store the objects
            earthObjs.append([subel])

    #here i am trying to find the object with the min 'approach_date'
    minEarth = min(dt.strptime(earthObjs["close_approach_date"],"%Y-%m-%d"))

    #then i would have to somehow place this as the only element of close_approach_data
    element["close_approach_data"] = json.loads(minEarth)

    #and clear the earthObjs list so it can be used for the next element
    earthObjs.clear()


我很清楚我的一半代码无法正常工作。我想我也许终于可以开始工作了,我只需要一些帮助。具体来说,我知道搜索最小值时做错了,因为我无法访问对象的'close_approach_data'字段。
另外,我也不确定json.load s行。

最佳答案

这是将您描述的处理过程相当简单地翻译为代码:

from datetime import datetime
import json

for dataset in data:
    earliest, initial = datetime.max, {}

    # Find the non-Earth body with the earliest approach date.
    for close_approach in dataset["close_approach_data"]:
        if close_approach["orbiting_body"] != "Earth":
            dt = datetime.strptime(close_approach["approach_date"],
                                   "%Y-%m-%d")
            if dt < earliest:
                dt, initial = earliest, close_approach

    # Replace entire close_approach_data list with a single object
    # comprised of the non-Earth item with the earliest date (or an
    # empty dictionary if there weren't any).
    dataset["close_approach_data"] = initial

print(json.dumps(data, indent=4))


输出:

[
    {
        "id": "01",
        "close_approach_data": {
            "orbiting_body": "Mars",
            "approach_date": "1935-06-07"
        }
    },
    {
        "id": "02",
        "close_approach_data": {
            "orbiting_body": "Mars",
            "approach_date": "1935-06-07"
        }
    }
]

关于python - 删除所有嵌套的JSON数组(日期最小的数组除外),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50282616/

10-14 05:53