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 문서의 맨 위에 있다고 가정하지만 실제로는 field 내에 포함되어 있습니다 organic_data
. 따라서 .[]
애초에 필요하지 않습니다 .organic_data[]
.