Search code examples
jsonwindowsbatch-fileexport-to-csvjq

How to concatenate an array in json using JQ


trying to combine the rows. Here is the source

{
  "movie_results": [
    {
      "genre_ids": [
        28,
        35,
        80
      ],
      "id": 96,
    }
  ]

Here is my command line

C:\WINDOWS\system32>curl "https://api.themoviedb.org/3/find/tt0092644?&external_source=imdb_id" | jq -r ".movie_results[] | .id, (.genre_ids | join(\",\"))

Im getting the following result

96
28,35,80

how do I make it 96,28,35,80? By the way im doing this on windows command line


Solution

  • A small tweak of your jq filter would do the trick:

    .movie_results[] | [.id, .genre_ids[]] | join(",")
    

    (The above line does not take into account your shell's rules for escaping special characters. Have you considered circumventing such issues by using the -f command-line option?)

    An alternative

    Here's an alternative that avoids using double quotes (") in the filter:

    .movie_results[] | [.id] + .genre_ids | @csv