1

I try to convert my .json files to .csv with jq. I'm getting cannot index array with string value on all my values. .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
    }
  ]
}

Command:

jq -r '.[] | [.description, .title, .link, .position] | @csv' input.json >> output.csv

I get the error:

jq: error (at input.json:45): Cannot index array with string "description"

With the follow command I get no error but the output is wrong

jq -r '.[] | [".description", ".title", ".link", ".position"] | @csv' input.json

Output:

".description",".title",".link",".position"

What I'm doing wrong and how to handle this correctly?

1 Answer 1

3

Your jq program assumes the array is at the top of the JSON document, but it's actually contained in the organic_data field. So, instead of .[] at the begining, you need .organic_data[].

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .