python - 在python中递归地删除json对象列表中的null/空值

标签 python json string list null

我有一个 json 对象(json 字符串),其值如下:

[
   {
      "id": 1,
      "object_k_id": "",
      "object_type": "report",
      "object_meta": {
         "source_id": 0,
         "report": "Customers"
      },
      "description": "Daily metrics for all customers",
      "business_name": "",
      "business_logic": "",
      "owners": [
         "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2e40406e4f4c4d004d4143" rel="noreferrer noopener nofollow">[email protected]</a>",
          null
      ],
      "stewards": [
         "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="94fafad4f5f6f7baf7fbf9" rel="noreferrer noopener nofollow">[email protected]</a>",
         ''
      ],
      "verified_use_cases": [
         null,
         null,
         "c4a48296-fd92-3606-bf84-99aacdf22a20",
         null
      ],
      "classifications": [
         null
      ],
      "domains": []
   }
]

我想要的最终格式是删除了空值和空列表项的格式:如下所示:

[
   {
      "id": 1,
      "object_k_id": "",
      "object_type": "report",
      "object_meta": {
         "source_id": 0,
         "report": "Customers"
      },
      "description": "Daily metrics for all customers",
      "business_name": "",
      "business_logic": "",
      "owners": [
         "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cca2a28cadaeafe2afa3a1" rel="noreferrer noopener nofollow">[email protected]</a>"
      ],
      "stewards": [
         "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="600e0e200102034e030f0d" rel="noreferrer noopener nofollow">[email protected]</a>"
      ],
      "verified_use_cases": [
         "c4a48296-fd92-3606-bf84-99aacdf22a20"
      ],
      "classifications": [],
      "domains": []
   }
]

我希望输出排除空值、空字符串并使其看起来更干净。 我需要对我拥有的所有 json 中的所有列表递归地执行此操作。

比递归更重要的是,如果我可以一次性完成而不是循环遍历每个元素,那将会很有帮助。

不过我只需要清理列表。

有人可以帮我解决这个问题吗?提前致谢

最佳答案

import json


def recursive_dict_clean(d):
    for k, v in d.items():
        if isinstance(v, list):
            v[:] = [i for i in v if i]
        if isinstance(v, dict):
            recursive_dict_lookup(v)


data = json.loads("""[{
    "id": 1,
    "object_k_id": "",
    "object_type": "report",
    "object_meta": {
        "source_id": 0,
        "report": "Customers"
    },
    "description": "Daily metrics for all customers",
    "business_name": "",
    "business_logic": "",
    "owners": [
        "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e28c8ca2838081cc818d8f" rel="noreferrer noopener nofollow">[email protected]</a>",
        null
    ],
    "stewards": [
        "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a64644a6b686924696567" rel="noreferrer noopener nofollow">[email protected]</a>"
    ],
    "verified_use_cases": [
        null,
        null,
        "c4a48296-fd92-3606-bf84-99aacdf22a20",
        null
    ],
    "classifications": [
        null
    ],
    "domains": []
}]""")


for d in data:
    recursive_dict_clean(d)

print(data):
[{'id': 1,
  'object_k_id': '',
  'object_type': 'report',
  'object_meta': {'source_id': 0, 'report': 'Customers'},
  'description': 'Daily metrics for all customers',
  'business_name': '',
  'business_logic': '',
  'owners': ['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b75755b7a797835787476" rel="noreferrer noopener nofollow">[email protected]</a>'],
  'stewards': ['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bed0d0fedfdcdd90ddd1d3" rel="noreferrer noopener nofollow">[email protected]</a>'],
  'verified_use_cases': ['c4a48296-fd92-3606-bf84-99aacdf22a20'],
  'classifications': [],
  'domains': []}]

P.S.:您的 json 字符串无效。

关于python - 在python中递归地删除json对象列表中的null/空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71732143/

相关文章:

python - 为 Web 服务器返回 html 而不是 python 中的纯文本

python - 类型错误 : Dimensions of C are incompatible with X Y

php - 编码一个完美的json

Java:查找最长的连续相同字符数组

c++ - 指针在字符串C++中的输出位置

python - 将字符串列表转换为 int

php - 有没有办法以编程方式访问集市存储库?

python - 如何将修改响应正文的 WSGI 中间件添加到 AppEngine

sql - Postgres 中的 GROUP BY - JSON 数据类型不相等?

c++ - 如何在 json-Glib 中打印 Jsonarray?