我尝试使用 jq 将 .json 文件转换为 .csv。我得到无法索引数组与我的所有值的字符串值。 .json
{
"organic_data": [
{
"description": "Football news, scores, results, fixtures and videos from the Premier League, Championship, European and World Football from the BBC.",
"title": "Football - BBC Sport",
"link": "https://www.bbc.co.uk/sport/football",
"position": 0
},
{
"description": "Sky Sports Football - Live games, scores, latest football news, transfers, results, fixtures and team news from the Premier to the Champions League.",
"title": "Football Games, Results, Scores, Transfers, News - Sky Sports",
"link": "https://www.skysports.com/football",
"position": 1
},
{
"description": "Football news, results, fixtures, blogs, podcasts and comment on the Premier League, European and World football from the Guardian, the world's leading ...",
"title": "Soccer news, match reports and fixtures | The Guardian",
"link": "https://www.theguardian.com/football",
"position": 2
},
{
"description": "What's happening in the grassroots game? Stay up-to-date or find out how you can participate in football via our England Football pages. The FA ...",
"title": "The website for the English Football Association, Emirates FA ...",
"link": "https://www.thefa.com/",
"position": 3
},
{
"description": "",
"title": "Football - Wikipedia",
"link": "https://en.wikipedia.org/wiki/Football",
"position": 4
},
{
"description": "Association football, more commonly known as football or soccer, is a team sport played between two teams of 11 players who primarily use their feet to ...",
"title": "Association football - Wikipedia",
"link": "https://en.wikipedia.org/wiki/Association_football",
"position": 5
},
{
"description": "",
"title": "Football news - transfers, fixtures, scores, pictures | The Sun",
"link": "https://www.thesun.co.uk/sport/football/",
"position": 6
}
]
}
命令:
jq -r '.[] | [.description, .title, .link, .position] | @csv' input.json >> output.csv
我收到错误:
jq: error (at input.json:45): Cannot index array with string "description"
使用以下命令我没有收到错误,但输出是错误的
jq -r '.[] | [".description", ".title", ".link", ".position"] | @csv' input.json
输出:
".description",".title",".link",".position"
我做错了什么以及如何正确处理?
答案1
您的 jq 程序假设数组位于 JSON 文档的顶部,但它实际上包含在字段中organic_data
。因此,.[]
您不需要在一开始就需要.organic_data[]
.