简体   繁体   English

如何在 python 的 json 响应中遍历所有对象

[英]how can I iterate through all objects in a json response in python

I have been banging my head pretty hard against the wall trying to figure out why I can get some attributes but not all in a json response(in Python)我一直在努力想弄清楚为什么我可以在 json 响应中获得一些属性但不能全部获得(在 Python 中)

For example: when running the code in the below function, the 3 attributes I am getting here and found successfully(_id, state, type, and name) are all on the same level as some other attributes, however it seems I cannot get the other attributes.例如:运行下面 function 中的代码时,我在这里找到并成功找到的 3 个属性(_id、state、类型和名称)都与其他一些属性处于同一级别,但似乎我无法获得其他属性。 (See full json response below the function.) (请参阅 function 下方的完整 json 响应。)

Values I cant get are for scan_progress_status, parent_scan_id, parent_scan_type我无法获得的值为 scan_progress_status、parent_scan_id、parent_scan_type

getScans = scans.get_scans()
scanResults = json.loads(getScans)
results = {}

for x in scanResults['scans']:
    results.update({x['_id'][0:]: {
       'state': x['state'],
       'name': x['name'],
       'type': x['type'],
       'parent_scan_id': x['parent_scan_id']
       
    }
})
y = json.dumps(results)
scanStates = cj.convert_pretty_json(y)

return scanStates

RESPONSE回复

    {
  "scans": [
    {
      "_id": "60c40198152afeca2e5f2cfd",       
      "classification_is_enabled": true,       
      "connectorType": "rdb-postgresql",       
      "created_at": "2021-06-12T00:36:40.322Z",
      "customConnectorType": "default",        
      "ds_connection_name": "client_id_test",  
      "ds_location": null,
      "identityConnectionsUuid": [],
      "isDifferential": false,
      "isSampling": false,
      "name": "GenericScanner Subscan 1/1: client_id_test",
      "number_of_parsing_threads": 2,
      "origin": "client_id_test",
      "parent_scan_id": "60c40198152afeca2e5f2ce7",
      "parent_scan_type": "full_scan",
      "profile_id": null,
      "row_identifier_expression_is_disabled": false,
      "scanSecretKey": "masked0a0VT24aOvlg==",
      "scan_progress_status": {
        "Queued": "2021-06-12T00:36:40.322Z",
        "CollectingMetadata": "2021-06-12T00:36:44.239Z"
      },
      "scanner_group": "default",
      "scanner_type_group": "structured",
      "state": "CollectingMetadata",
      "structured_clustering_enabled": false,
      "type": "sub_scan",
      "updated_at": "2021-06-12T00:36:44.364Z",
      "startTimestamp": 1623458200354,
      "enumerationState": "Completed",
      "scan_enumeration_progress_status": {
        "Started": "2021-06-12T00:36:44.239Z",
        "Completed": "2021-06-12T00:36:44.364Z"
      },
      "scannerId": "4340cbb7-5949-4412-8ddf-afd36dd16d1c",
      "latestCollectionScanned": {
        "fullyQualifiedName": "client_id_test.public.clients",
        "totalRows": null,
        "totalRowsWithFindings": null
      },
      "scannedSize": 0,
      "totalCollections": 0,
      "totalEnumerated": 1,
      "totalFailedCollections": 0,
      "totalFindings": 0,
      "totalRows": 0,
      "totalRowsWithFindings": 0,
      "scan_parts_counter": {
        "New": 1
      }]}

What makes parent_scan_id, and some of the others different enough that it would cause Key Errors?是什么让 parent_scan_id 和其他一些不同之处足以导致关键错误?

The answer turned out to be the attributes like 'parent_scan_id" where I was getting key errors was because some blocks did not have this attribute.答案原来是像“parent_scan_id”这样的属性,我遇到关键错误是因为某些块没有这个属性。

The Solution was to set a variable to parent_scan_id, and if it exists then update my results object解决方案是将变量设置为 parent_scan_id,如果存在则更新我的结果 object

 for x in scanResults['scans']:
    parent_scan_id = x['parent_scan_id'] if 'parent_scan_id' in x else 'undefined'
    results.update({x['_id'][0:]: {
       'state': x['state'],
       'name': x['name'],
       'type': x['type'],
       'parent_scan_id': parent_scan_id
       
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM