簡體   English   中英

將具有名稱和值的 JSON 轉換為 TSV?

[英]Convert JSON with both names and values to TSV?

對於以下輸入 JSON,我想像這樣獲得 output(▸ 用於 TAB,¬ 用於換行)。 我不知道這樣做的最佳方法是什么。 誰能告訴我從 JSON 輸入中提取此類信息的最簡潔方法是什么? 謝謝。

Gene.primaryIdentifier▸ Gene.name▸  Gene.symbol▸    Gene.description▸   Gene.chr▸   Gene.mitoEvidenceMitoCarta▸ Gene.mitoEvidenceIMPI▸  Gene.mitoEvidenceGO▸    Gene.mitoEvidenceHumanProteinAtlas▸ Gene.MTSipsort▸ Gene.MTSmitofates▸  Gene.MTSmitoprot▸   Gene.MTStargetP¬
ENSG00000002330▸    572▸    BAD▸    BCL2 associated agonist of cell death▸  11▸ true▸   Known mitochondrial▸    true▸   true▸   0▸  0▸  0.0088▸ 0.064¬
...
{
  "rootClass": "Gene",
  "modelName": "genomic",
  "start": 0,
  "views": [
    "Gene.primaryIdentifier",
    "Gene.name",
    "Gene.symbol",
    "Gene.description",
    "Gene.chr",
    "Gene.mitoEvidenceMitoCarta",
    "Gene.mitoEvidenceIMPI",
    "Gene.mitoEvidenceGO",
    "Gene.mitoEvidenceHumanProteinAtlas",
    "Gene.MTSipsort",
    "Gene.MTSmitofates",
    "Gene.MTSmitoprot",
    "Gene.MTStargetP"
  ],
  "iTotalRecords": 1157,
  "columnHeaders": [
    "Gene > Ensembl Primary Identifier",
    "Gene > NCBI Gene ID",
    "Gene > Gene Symbol",
    "Gene > Description",
    "Gene > Chromosome",
    "Gene > Mito Evidence MitoCarta",
    "Gene > Mito Evidence IMPI",
    "Gene > Mito Evidence GO Annotation",
    "Gene > Mito Evidence Human Protein Atlas",
    "Gene > Mito Targeting Seq iPSORT",
    "Gene > Mito Targeting Seq MitoFates",
    "Gene > Mito Targeting Seq MitoProt",
    "Gene > Mito Targeting Seq TargetP"
  ],
  "results": [
    [
      {
        "id": 1026800,
        "column": "Gene.primaryIdentifier",
        "value": "ENSG00000002330",
        "class": "Gene",
        "url": "/report.do?id=1026800"
      },
      {
        "id": 1026800,
        "column": "Gene.name",
        "value": "572",
        "class": "Gene",
        "url": "/report.do?id=1026800"
      },
      {
        "id": 1026800,
        "column": "Gene.symbol",
        "value": "BAD",
        "class": "Gene",
        "url": "/report.do?id=1026800"
      },
      {
        "id": 1026800,
        "column": "Gene.description",
        "value": "BCL2 associated agonist of cell death",
        "class": "Gene",
        "url": "/report.do?id=1026800"
      },
      {
        "id": 1026800,
        "column": "Gene.chr",
        "value": "11",
        "class": "Gene",
        "url": "/report.do?id=1026800"
      },
      {
        "id": 1026800,
        "column": "Gene.mitoEvidenceMitoCarta",
        "value": true,
        "class": "Gene",
        "url": "/report.do?id=1026800"
      },
      {
        "id": 1026800,
        "column": "Gene.mitoEvidenceIMPI",
        "value": "Known mitochondrial",
        "class": "Gene",
        "url": "/report.do?id=1026800"
      },
      {
        "id": 1026800,
        "column": "Gene.mitoEvidenceGO",
        "value": true,
        "class": "Gene",
        "url": "/report.do?id=1026800"
      },
      {
        "id": 1026800,
        "column": "Gene.mitoEvidenceHumanProteinAtlas",
        "value": true,
        "class": "Gene",
        "url": "/report.do?id=1026800"
      },
      {
        "id": 1026800,
        "column": "Gene.MTSipsort",
        "value": 0,
        "class": "Gene",
        "url": "/report.do?id=1026800"
      },
      {
        "id": 1026800,
        "column": "Gene.MTSmitofates",
        "value": 0,
        "class": "Gene",
        "url": "/report.do?id=1026800"
      },
      {
        "id": 1026800,
        "column": "Gene.MTSmitoprot",
        "value": 0.0088,
        "class": "Gene",
        "url": "/report.do?id=1026800"
      },
      {
        "id": 1026800,
        "column": "Gene.MTStargetP",
        "value": 0.064,
        "class": "Gene",
        "url": "/report.do?id=1026800"
      }
    ],
    [
      {
        "id": 1025305,
        "column": "Gene.primaryIdentifier",
        "value": "ENSG00000002549",
        "class": "Gene",
        "url": "/report.do?id=1025305"
      },
      {
        "id": 1025305,
        "column": "Gene.name",
        "value": "51056",
        "class": "Gene",
        "url": "/report.do?id=1025305"
      },
      {
        "id": 1025305,
        "column": "Gene.symbol",
        "value": "LAP3",
        "class": "Gene",
        "url": "/report.do?id=1025305"
      },
      {
        "id": 1025305,
        "column": "Gene.description",
        "value": "leucine aminopeptidase 3",
        "class": "Gene",
        "url": "/report.do?id=1025305"
      },
      {
        "id": 1025305,
        "column": "Gene.chr",
        "value": "4",
        "class": "Gene",
        "url": "/report.do?id=1025305"
      },
      {
        "id": 1025305,
        "column": "Gene.mitoEvidenceMitoCarta",
        "value": true,
        "class": "Gene",
        "url": "/report.do?id=1025305"
      },
      {
        "id": 1025305,
        "column": "Gene.mitoEvidenceIMPI",
        "value": "Known mitochondrial",
        "class": "Gene",
        "url": "/report.do?id=1025305"
      },
      {
        "id": 1025305,
        "column": "Gene.mitoEvidenceGO",
        "value": false,
        "class": "Gene",
        "url": "/report.do?id=1025305"
      },
      {
        "id": 1025305,
        "column": "Gene.mitoEvidenceHumanProteinAtlas",
        "value": false,
        "class": "Gene",
        "url": "/report.do?id=1025305"
      },
      {
        "id": 1025305,
        "column": "Gene.MTSipsort",
        "value": 1,
        "class": "Gene",
        "url": "/report.do?id=1025305"
      },
      {
        "id": 1025305,
        "column": "Gene.MTSmitofates",
        "value": 0.997,
        "class": "Gene",
        "url": "/report.do?id=1025305"
      },
      {
        "id": 1025305,
        "column": "Gene.MTSmitoprot",
        "value": 0.9979,
        "class": "Gene",
        "url": "/report.do?id=1025305"
      },
      {
        "id": 1025305,
        "column": "Gene.MTStargetP",
        "value": 0.946,
        "class": "Gene",
        "url": "/report.do?id=1025305"
      }
    ]
  ],
  "executionTime": "2019.10.26 03:00::38",
  "wasSuccessful": true,
  "error": null,
  "statusCode": 200
}

如果 .views 和 .results 的順序一致,那么您可以使用這個 jq 過濾器(結合 -r 命令行選項):

.views,
 (.results[] | map(.value))
| @tsv

但是,由於這是一個很大的假設,因此最好采用更穩健的解決方案,例如:

def gather($ary):
  INDEX(.[]; .column) as $dict
  | $ary
  | map( $dict[.] | .value );

.views as $views
| ($views,
   (.results[] | gather($views)))
| @tsv

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM