Using a pared-down sample JSON object with just the relevant key:

$ cat test.json
{
    "drop_reasons": ["a","b","c"]
}
$ jq '.drop_reasons |= join(",")' test.json
{
  "drop_reasons": "a,b,c"
}

Your sample with an empty array would change to an empty string.


x |= y is essentially shorthand for x = (x | y). The parens are what you were missing in your attempt; they're needed because of jq precedence rules.

Answer from Shawn on Stack Overflow
🌐
Qmacro
qmacro.org › blog › posts › 2022 › 05 › 06 › converting-strings-to-objects-with-jq
Converting strings to objects with jq - DJ Adams
May 6, 2022 - Moreover, given that I had the elements where I wanted them, in an outer array, it would seem sensible at this point onwards to express the transformations required via map, which (like map in other languages, I guess it's as much of a paradigm as it is a function or filter), takes an array and produces an array. So for example, I could replace each string with its length, while still keeping the structure, by passing the [.,inputs] into map like this: ... This would produce the following (note I've used the --compact-output (-c) option to save space here): ... In the modification requirements, I first had to remove the SAP-samples/ organisation name prefix, and I turned to sub for that, as I'm partial to the occasional regular expression: jq -R '[.,inputs] | map(sub("^.+/";""))' names.txt
Discussions

Transform a list of strings to JSON array using jq - Stack Overflow
Having a list of strings (one per line) like str1 str2 ... how do I convert these into the JSON list ["str1", "str2", ...]? More on stackoverflow.com
🌐 stackoverflow.com
bash - convert JSON array to space separated string - Unix & Linux Stack Exchange
I need to parse this JSON so the array of strings inside JSON can convert to "space separated string". I later need to supply these variables to another function like this: local file="file-name" while read val; do local name local source name=$(jq --raw-output '.name' <<< ${val}) source=$(jq ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
June 23, 2021
json - Using jq to convert array of strings into array of objects - Stack Overflow
I can read the first elements of both arrays with: jq '.[] as $o | $o.Authors | split(", ") as $authors | $o.Affiliation | split(", ") as $affiliation | { "Authors": [ { "Name": $authors[.0], "Affiliation": $affiliation[.0]} ] }' but I'm struggling to understand how to get jq to iterate over the entire (arbitrary length) string ... More on stackoverflow.com
🌐 stackoverflow.com
February 7, 2018
How to filter using jq array of strings which contains a specific value - Stack Overflow
When you have a syntax error, it's good to state what it is exactly. In this case, I get jq: error: syntax error, unexpected $end, expecting ';' or ')' which pretty much tells you the problem, that you're missing a closing parenthesis. More on stackoverflow.com
🌐 stackoverflow.com
🌐
jq
jqlang.org › manual
jq 1.8 Manual
If no mutation is applied to this ... in JSON, [] is used to construct arrays, as in [1,2,3]. The elements of the arrays can be any jq expression, including a pipeline....
🌐
jq recipes
remysharp.com › drafts › jq-recipes
jq recipes
April 16, 2024 - In this case, running Zeit's now ls | jq --raw-input --slurp to find out how many running instance I have: split("\n")[1:-3] | # split into an array of strings, removing the 1st and last few blank lines map([ split(" ")[] | select(.
🌐
Exercism
exercism.org › tracks › jq › concepts › arrays
Arrays in jq on Exercism
There are circumstances where we want to stream the elements of an array individually. For this we use .[]. Suppose we want a stream of the lengths of the strings in an array.
🌐
Zendesk Developer Docs
developer.zendesk.com › documentation › integration-services › developer-guide › jq-cheat-sheet
jq cheat sheet | Zendesk Developer Docs
To create an array, wrap the output in square brackets ([]). The following expression combines the collaborator_ids and submitter_id properties into a single array. ... Use the index filter to get the index of the first occurrence of an element in an array. If the element doesn't exist in the ...
🌐
GitHub
github.com › jqlang › jq › issues › 66
SUGGESTIONS: string manipulation as arrays; array ranges; more examples · Issue #66 · jqlang/jq
One way is to make the .[] indexing and length also apply to strings (I note that contains already applies to strings). A way to do this is a toarray function (and also the reverse, arraytostring e.g. $ echo ' "cat" ' | jq 'toarray | ., length, .[0], arraytostring' [ "c", "a", "t" ] 3 "c" "cat" It would also be nice to extract a range of an array (and also of a string, via toarray).
Author   ghost
Find elsewhere
🌐
GitHub
gist.github.com › olih › f7437fb6962fb3ee9fe95bda8d2c8fa4
jq Cheet Sheet · GitHub
-> % jq '[path(..)] | .[3] ' example.json [ "data", "object", "user" ] (ehsm-py3.9) cbongior@cbongior-mac [09:54:38] [~/dev/ehsm] [main *] -> % jq 'getpath([ "data", "object", "user" ])' example.json { "id": 1, "range": [ -255, 0, 255 ], "notation": "big-O", "details": { "lat": 0.000, "long": 0.000, "time": 42 } } (ehsm-py3.9) cbongior@cbongior-mac [09:55:03] [~/dev/ehsm] [main *] -> % jq '[path(..)] | .[3] | getpath' example.json jq: error: getpath/0 is not defined at <top-level>, line 1: [path(..)] | .[3] | getpath jq: 1 compile error (ehsm-py3.9) cbongior@cbongior-mac [09:55:25] [~/dev/ehsm] [main *] -> % jq '[path(..)] | .[3] | getpath(.)' example.json jq: error (at example.json:27): Cannot index array with string "data"
🌐
GitHub
github.com › jqlang › jq › issues › 189
Allow "raw" output of arrays · Issue #189 · jqlang/jq
October 10, 2013 - When using jq -r, if the result is an array, then assume it's an array of strings, and print them, one per line, without quotes, without commas, and without a leading [ or trailing ]. The goal is that this: echo '{"results":[{"id":1},{"i...
Author   arodland
🌐
jq
jqlang.org › tutorial
Tutorial
If you want to get the output as a single array, you can tell jq to "collect" all of the answers by wrapping the filter in square brackets: jq '[.[] | {message: .commit.message, name: .commit.committer.name}]' Show resultHide result · [ { "message": "docs: Document repeat(exp)", "name": "Nico Williams" }, { "message": "Mention -n in IO-section and for input/inputs", "name": "Nico Williams" }, { "message": "Fix iteration problem for non decimal string\n\nWhen the string transformation to number failed, all following\ntransformation failed too.\n\nThis happend because status in decNumberFromString function is\nupdated just in error case.
🌐
Programming Historian
programminghistorian.org › en › lessons › json-and-jq
Reshaping JSON with jq | Programming Historian
May 24, 2016 - Tweet ids, on the other hand, are ... we need to convert our integers into strings before using the join() command in the next step. Both of these commands are wrapped in [] which tells jq to collect every result into one single array, which is passed with a | along ...
🌐
Learn X in Y Minutes
learnxinyminutes.com › jq
Learn jq in Y Minutes
Feel free to look # them up in ... their # averages, and finally generate a report. # echo $numbers | jq -rs ' # Slurp the numbers into an array. [ [ map(tostring) # Turn it into an array of strings....