【问题标题】:How do i filter nested json file using python?如何使用 python 过滤嵌套的 json 文件?
【发布时间】:2021-05-24 18:44:48
【问题描述】:

我正在尝试过滤嵌套的 JSON 文件。我想创建一个带有拟合“类”和“id”的新 json 文件。源 JSON 文件:

[
{"data": {"id": "ed", "label": "Executive Director (Harriet)"},
 "classes": "purple"  
 },

{"data": {"id": "vp1", "label": "Vice President (Sarah)"},
 "classes": "square"  
 },

{"data": {"id": "vp2", "label": "Vice President (Charlotte)"},
 "classes": "square"  
 },

{"data": {"id": "po1", "label": "Program Officer (Sojourner)"},
 "classes": "green diamond"  
 },

 {"data": {"id": "po2", "label": "Program Officer (Sojourner)"},
 "classes": "green diamond"  
 },
{"data": {"id": "pa", "label": "Program Associate (Ellen)"},
 "classes": "myimage"  
 }
 ]

我的目标是过滤 'classes': 'green diamond' 具有 'id': 'po1' 。然后删除所有带有 'green diamond' 的类,除了 'id': 'po1'

结果:

[
{"data": {"id": "ed", "label": "Executive Director (Harriet)"},
 "classes": "purple"  
 },

{"data": {"id": "vp1", "label": "Vice President (Sarah)"},
 "classes": "square"  
 },

{"data": {"id": "vp2", "label": "Vice President (Charlotte)"},
 "classes": "square"  
 },

{"data": {"id": "po1", "label": "Program Officer (Sojourner)"},
 "classes": "green diamond"  
 },


{"data": {"id": "pa", "label": "Program Associate (Ellen)"},
 "classes": "myimage"  
 }]

我尝试使用此代码来获取数据,但它引发了错误:

import json

# Loding the data
with open("D:/bb.json", 'r') as file:
    content = file.read()

# Converting json_data to python dictionary format
json_data = json.loads(content)

quantite = -1  # -1 for not found case
for data in json_data[0]:

    # Checking for specific pair
    if data['classes'] == 'square' and data['id'] == 'vp2':
        print(data)
        break

如何过滤这样的 json 文件?

【问题讨论】:

  • 如果你要编写 json,请使用 json.dumps 并执行类似 json.dumps([ x for x in json_data if x['classes'] == 'square' and x[' id'] == 'vp2'])

标签: python json python-3.x


【解决方案1】:

由于加载的json数据只是嵌套的lists和dicts,可以使用普通的list/dict操作;特别是,列表推导很有用。

import json

with open('input', 'r') as f:
    data = json.loads(f.read())

filtered = [x for x in data if not (x['classes'] == 'green diamond' and x['data']['id'] != 'po1')]

with open('output', 'w') as f:
    f.write(json.dumps(filtered, indent=2))

结果:

[
  {
    "data": {
      "id": "ed",
      "label": "Executive Director (Harriet)"
    },
    "classes": "purple"
  },
  {
    "data": {
      "id": "vp1",
      "label": "Vice President (Sarah)"
    },
    "classes": "square"
  },
  {
    "data": {
      "id": "vp2",
      "label": "Vice President (Charlotte)"
    },
    "classes": "square"
  },
  {
    "data": {
      "id": "po1",
      "label": "Program Officer (Sojourner)"
    },
    "classes": "green diamond"
  },
  {
    "data": {
      "id": "pa",
      "label": "Program Associate (Ellen)"
    },
    "classes": "myimage"
  }
]

【讨论】:

    【解决方案2】:

    尝试以下,对您的代码进行一些更新

    import json
    # Loding the data
    pathToFile = "bb.json"
    with open(pathToFile, 'r') as file:
        content = file.read()
    # Converting json_data to python dictionary format
    json_data = json.loads(content)
    
    removeEntryClass = "green diamond"
    keepId = "po1"
    outputList = []
    
    for entry in json_data:
        if entry["classes"] == removeEntryClass and entry["data"]['id'] != keepId :
            continue
        outputList.append(entry)
    
    print(outputList)
    #You can save this list into jour file.. 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 2015-02-17
      • 1970-01-01
      • 2018-08-12
      • 2022-01-19
      相关资源
      最近更新 更多