json jq 将未命名对象格式化为单行

问题描述 投票:0回答:2

另一个问题是将“jq”格式化为一行。这是我的 json 文件:

  "facet_counts":{
    "facet_queries":{},
    "facet_fields":{
      "title":[
        "primary",5981,
        "database",5965,
        "source",5963,
        "eecm",5949,
        "the",5066,
        "research",4888]},
    "facet_ranges":{}
  }
}

如您所见,标题数组中没有为连音分配标签。我想打印输出,例如:-

        "primary",5981,
        "database",5965,
        "source",5963,
        "eecm",5949,
        "the",5066,
        "research",4888

到目前为止,我一直在尝试 jq -c '.facet_counts.facet_fields.title[]',但是连元组的成员正在不同的行上输出(即有效解耦):-

"primary"
5981
"database"
5965
"source"
5963
"eecm"
5949
"the"
5066
"research"
4888
json jq
2个回答
0
投票

只需通过管道传输

jq
的输出即可对其进行格式化:

jq ... | paste -d, - - 

0
投票

您可以使用

_nwise
将数组分成块,然后使用
join()
那些具有您喜欢的字符的块:

.facet_counts.facet_fields.title | _nwise(2) | join(": ")
primary: 5981
database: 5965
source: 5963
eecm: 5949
the: 5066
research: 4888

JqPlay 演示
© www.soinside.com 2019 - 2024. All rights reserved.