jq: エラー (input.json:45): 'value' 文字列を使用して配列をインデックスできません。

jq: エラー (input.json:45): 'value' 文字列を使用して配列をインデックスできません。

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[]

おすすめ記事