You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在指定JSON数据中找出返回值为False的键?

Find Keys with False Values in Nested JSON Data

Let's work through how to identify all keys that have a value of False in your provided new_dto_data structure. Since the data has nested dictionaries (like Product and Device), we'll need a recursive approach to check every level of the structure.

Step 1: Create a Helper Function to Traverse Nested Data

This function will dig through lists and dictionaries, collecting all keys where the value is strictly False—including those in nested layers. It also tracks the full path to each key for clarity:

def find_false_keys(data, parent_key=""):
    false_keys = []
    # Handle lists of dictionaries (like your top-level new_dto_data)
    if isinstance(data, list):
        for item in data:
            false_keys.extend(find_false_keys(item, parent_key))
    # Handle individual dictionaries
    elif isinstance(data, dict):
        for key, value in data.items():
            # Build the full key path (e.g., Device.Name.IsUnique)
            full_key = f"{parent_key}.{key}" if parent_key else key
            # Check if the value is exactly False
            if value is False:
                false_keys.append(full_key)
            # Recurse into nested dictionaries or lists
            elif isinstance(value, (dict, list)):
                false_keys.extend(find_false_keys(value, full_key))
    return false_keys

Step 2: Integrate into Your Test Script

Now you can plug this helper into your existing test() function to collect and validate the false keys. Here's the full updated script:

new_dto_data = [
    {
        'DeviceInstanceId': 24,
        'IsResetNeeded': False,
        'ProductType': 'SLDW',
        'Product': {'Family': 'SL DW'},
        'Device': {
            'DeviceFirmwareUpdate': {
                'DeviceUpdateStatus': None,
                'DeviceUpdateInProgress': None,
                'DeviceUpdateProgress': None,
                'LastDeviceUpdateId': None
            },
            'ManualAdded': False,
            'Name': {'DeviceName': 'SLDW25', 'IsUnique': False},
            'State': 0,
            'Location': 'Room'
        }
    }
]

def test():
    """Validate that the 0 or more Payload dicts in record use proper types"""
    # Collect all keys with False values
    false_keys = find_false_keys(new_dto_data)
    
    # Example: Handle results (customize this for your err_path logic)
    if false_keys:
        print("Keys with False value detected:", false_keys)
        # Add your error reporting/validation logic here
    else:
        print("No keys with False value found")

# Execute the test
test()

Step 3: Check the Output

When you run this script, you'll get the full path to every key with a False value:

Keys with False value detected: ['IsResetNeeded', 'Device.ManualAdded', 'Device.Name.IsUnique']

This makes it easy to pinpoint exactly where the False values live in your nested data structure.

内容的提问来源于stack exchange,提问作者nrs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:09:37