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

时间:2019-10-26 02:09:03

标签: json export-to-csv jq

对于以下输入JSON,我想获得这样的输出(▸用于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
}

1 个答案:

答案 0 :(得分:0)

如果.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