简体   繁体   English

将具有名称和值的 JSON 转换为 TSV?

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

For the following input JSON, I'd like to get the output like this (▸ is for TAB, and ¬ is for newline).对于以下输入 JSON,我想像这样获得 output(▸ 用于 TAB,¬ 用于换行)。 I don't know what is the best way to do so.我不知道这样做的最佳方法是什么。 Could you anybody show me what is the most succinct way to extract such info from the JSON input?谁能告诉我从 JSON 输入中提取此类信息的最简洁方法是什么? Thanks.谢谢。

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
}

If the.views and.results are ordered consistently, then you could get away with this jq filter (in conjunction with the -r command-line option):如果 .views 和 .results 的顺序一致,那么您可以使用这个 jq 过滤器(结合 -r 命令行选项):

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

However, since that is a big assumption, it might be preferable to adopt a more robust solution, such as:但是,由于这是一个很大的假设,因此最好采用更稳健的解决方案,例如:

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