我有一个json文件。基于case_id值,如何访问相应的项目ID值?

时间:2018-10-27 14:14:33

标签: python json

[
  {
    "data_type": "Masked Somatic Mutation",
    "file_name": "TCGA.GBM.mutect.195dab4b-31ae-4e1d-9e76-ff95bd1f9a23.DR-6.0.somatic.maf.gz",
    "data_format": "MAF",
    "submitter_id": "TCGA-GBM-mutect-public",
    "access": "open",
    "file_id": "195dab4b-31ae-4e1d-9e76-ff95bd1f9a23",
    "data_category": "Simple Nucleotide Variation",
    "file_size": 19634323,
    "cases": [
      {
        "project": {
          "project_id": "TCGA-GBM"
        },
        "case_id": "987cb702-057b-4198-8ef3-9cf6a9c51989"
      },
      {
        "project": {
          "project_id": "TCGA-GBM"
        },
        "case_id": "cdfcf14d-ed6c-419f-88f5-e7dee96fcaf6"
      },
      {
        "project": {
          "project_id": "TCGA-GBM"
        },
        "case_id": "1e2108e8-b620-4865-a8d8-77f9a0fe28a2"
      },
      {
        "project": {
          "project_id": "TCGA-GBM"
        },
        "case_id": "aa7ad106-eadc-489e-83b2-5698652434ea"
      },

1 个答案:

答案 0 :(得分:0)

假设您的JSON对象存储为变量l

case_id = 'cdfcf14d-ed6c-419f-88f5-e7dee96fcaf6'
next((i['project']['project_id'] for d in l for i in d['cases'] if i['case_id'] == case_id), None)

返回:'TCGA-GBM'

或者如果找不到case_id,它将返回None