在for循环期间动态创建list / dict以转换为JSON

时间:2017-03-28 20:05:13

标签: python json python-2.7 python-3.x dictionary

我正在尝试构建一个稍后将转换为JSON的列表/字典。我正在尝试编写构建和填充我最终需要的JSON格式的多个级别的代码。我正在解决这个问题。谢谢你的帮助。

我最终需要什么 - >填充此列表/字典:

dataset_permission_json = []

采用以下格式:

{
   "projects":[
      {
         "project":"test-project-1",
         "datasets":[
            {
               "dataset":"testing1",
               "permissions":[
                  {
                     "role":"READER",
                     "google_group":"testing1@test.com"
                  }
               ]
            },
            {
               "dataset":"testing2",
               "permissions":[
                  {
                     "role":"OWNER",
                     "google_group":"testing2@test.com"
                  }
               ]
            },
            {
               "dataset":"testing3",
               "permissions":[
                  {
                     "role":"READER",
                     "google_group":"testing3@test.com"
                  }
               ]
            },
            {
               "dataset":"testing4",
               "permissions":[
                  {
                     "role":"WRITER",
                     "google_group":"testing4@test.com"
                  }
               ]
            }
         ]
      }
   ]
}

我有多个for循环,可以成功打印出我从外部API中提取的信息,但我能够将该数据输入到list / dict中。我想输入的动态值是:

'project' i.e. test-project-1
'dataset' i.e. testing1
'role' i.e. READER
'google_group' i.e. testing1@test.com

我尝试过这样的事情:

dataset_permission_json.update({'project': project})

但无法弄清楚在多个for循环期间如何不覆盖数据。

for project in projects:

    print(project) ## Need to add this variable to 'projects'

    for bq_group in bq_groups:
        delegated_credentials = credentials.create_delegated(bq_group)
        http_auth = delegated_credentials.authorize(Http())
        list_datasets_in_project = bigquery_service.datasets().list(projectId=project).execute()
        datasets = list_datasets_in_project.get('datasets',[])

        print(dataset['datasetReference']['datasetId']) ##Add the dataset to 'datasets' under the project

        for dataset in datasets:
            get_dataset_permissions_result = bigquery_service.datasets().get(projectId=project, datasetId=dataset['datasetReference']['datasetId']).execute()
            dataset_permissions = get_dataset_permissions_result.get('access',[])
            ### ADD THE NEXT LEVEL 'permissions' level here?
            for dataset_permission in dataset_permissions:
                if 'groupByEmail' in dataset_permission:
                    if bq_group in dataset_permission['groupByEmail']:

                        print(dataset['datasetReference']['datasetId'] && dataset_permission['groupByEmail']) ##Add to each dataset

我很感激帮助。

编辑:更新进度

好的我已经使用StackOverflow

创建了我正在寻找的嵌套结构

除了最后一部分,事情都很棒。我试图追加角色&分组到每个人的许可' nest,但在所有内容运行后,数据只会附加到最后的权限中。嵌套在JSON结构中。好像它在for循环期间覆盖了它自己。想法?

更新了循环:

for project in projects:

        for bq_group in bq_groups:
            delegated_credentials = credentials.create_delegated(bq_group)
            http_auth = delegated_credentials.authorize(Http())
            list_datasets_in_project = bigquery_service.datasets().list(projectId=project).execute()
            datasets = list_datasets_in_project.get('datasets',[])

            for dataset in datasets:
                get_dataset_permissions_result = bigquery_service.datasets().get(projectId=project, datasetId=dataset['datasetReference']['datasetId']).execute()
                dataset_permissions = get_dataset_permissions_result.get('access',[])
                for dataset_permission in dataset_permissions:
                    if 'groupByEmail' in dataset_permission:
                        if bq_group in dataset_permission['groupByEmail']:
                            dataset_permission_json['projects'][project]['datasets'][dataset['datasetReference']['datasetId']]['permissions']
                            permission = {'group': dataset_permission['groupByEmail'],'role': dataset_permission['role']}
                            dataset_permission_json['permissions'] = permission

更新:已解决。

dataset_permission_json['projects'][project]['datasets'][dataset['datasetReference']['datasetId']]['permissions']
permission = {'group': dataset_permission['groupByEmail'],'role': dataset_permission['role']}
dataset_permission_json['projects'][project]['datasets'][dataset['datasetReference']['datasetId']]['permissions'] = permission

0 个答案:

没有答案