【问题标题】:jq getting multiple fields from same queryjq 从同一个查询中获取多个字段
【发布时间】:2023-01-27 18:54:23
【问题描述】:

我有一个这种类型的 json:

{
  "_index": "indexeer",
  "_type": "_doc",
  "_id": "3233233233",
  "_score": 1,
  "_source": {
    "Bibtex": {
      "Article": {
        "AuthorList": [
          {
            "Affiliation": {
              "Affiliation": "Title content, Hematology and Hepatology Honorary Fellow, Honorary Member, American  Society. xxxyyy@hotmail.com."
            }
          }
        ]
      }
    }
  }
}

我像这样使用jq得到Affiliation:

jq -r '._source.Bibtex.Article.AuthorList[]? | .Affiliation.Affiliation | .[0:rindex(" Electronic address:")]' results.json

它工作得很好,并为我提供了我需要的隶属关系。

但是,我现在还需要 id 字段并生成如下所示的 csv:

"3233233233", "Title content, Hematology and Hepatology Honorary Fellow, Honorary Member, American  Society"

我可以像这样轻松地单独获取 ID:

jq -r '._id' results.json

但是,我如何组合这两个查询来生成一个由id 和affiliation 组成的输出?

本质上我想结合这两个查询。

【问题讨论】:

    标签: linux bash csv jq


    【解决方案1】:

    使用._id as $id将id保存为变量,然后您可以在任何您喜欢的地方使用$id:

    ._id as $id | ._source.Bibtex.Article.AuthorList[]? | .Affiliation.Affiliation | [ $id, .[0:rindex(" Electronic address:")] ] | @csv
    
    "3233233233","Title content, Hematology and Hepatology Honorary Fellow, Honorary Member, American  Society. xxxyyy@hotmail.com."
    

    https://jqplay.org/s/298G4GJ2Kng

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-07
      • 1970-01-01
      • 2022-10-04
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多