1

I'm trying to output data in a JSON file I have with a few thousand rows to CSV. The JSON components follow this general pattern:

{
  "name": "pickles",
  "aliases": [
    "birds"
  ]
}
{
  "name": "cheese",
  "aliases": [
    "cheese1",
    "cheese2"
  ]
}

Some of the aliases have more than 1 value (up to four) and some just have one. I'm looking for a CSV output to have two columns - one for name, and one for aliases, where the value in name will be in the name column and value for aliases will be in the aliases column (see below).

name,aliases
pickles,birds
cheese, cheese1
cheese, cheese2

I'm trying to figure out which jq command(s) to use to get this output, but I'm stuck with the error Cannot index string with string "name"

I've tried a few jq commands and I'm getting similar errors. Most recently I tried:

jq -r '.[] | . as {$name} | [$name, .name, .aliases] | @csv' filename.json

1 Answer 1

-1

You could use --slurp to read in the stream as an array:

$ jq -s -r '("name,aliases"), (.[] | "\(.name),\(.aliases[])")' filename.json
name,aliases
pickles,birds
cheese,cheese1
cheese,cheese2
Sign up to request clarification or add additional context in comments.

1 Comment

Why the downvote?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.