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
🌐
jq
jqlang.org › manual
jq 1.8 Manual
This is useful when using jq as a simple calculator or to construct JSON data from scratch. ... Don't parse the input as JSON. Instead, each line of text is passed to the filter as a string. If combined with --slurp, then the entire input is passed to the filter as a single long string. ... Instead of running the filter for each JSON object in the input, read the entire input stream into a large array and run the filter just once.
Discussions

json - Make JQ transform an array of objects to a string - Stack Overflow
Note that StateGroups (if they ... into a string of values separated by vertical bars. What I have tried has given me partial results, but nothing really does the job: cat json_file | jq -r '[ .ClassIdentifier, .StateMode, .StateGroups[] ]' cat json_file | jq -r '{ ClassIdentifier, StateMode } + ( .StateGroups[] | { StateGroupName, Status } ) ' cat json_file | jq -r ' [ .ClassIdentifier, .StateMode, .StateGroups |= sort_by( .StateGroupName ) ]' UPDATE: We have to use JQ 1.3, ... More on stackoverflow.com
🌐 stackoverflow.com
May 24, 2017
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
command line - Merge jq output into a comma separated string - Unix & Linux Stack Exchange
I am trying to curl some URL which returns a json file, then I want to parse hosts from it and create a comma separated string. I have the first part working curl -s -u "admin:admin" -H "X-Reque... More on unix.stackexchange.com
🌐 unix.stackexchange.com
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
🌐
Zendesk Developer Docs
developer.zendesk.com › documentation › integration-services › developer-guide › jq-cheat-sheet
jq cheat sheet | Zendesk Developer Docs
Tip: For an example action using this filter, refer to ZIS action: Converting a number to a string. ... The following expression adds a static property to a list of ids and returns the output as an array of objects.
🌐
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
🌐
jq
jqlang.org › manual › v1.6
jq 1.6 Manual
Note that value will be treated as a string, so --arg foo 123 will bind $foo to "123". Named arguments are also available to the jq program as $ARGS.named. ... This option passes a JSON-encoded value to the jq program as a predefined variable. If you run jq with --argjson foo 123, then $foo is available in the program and has the value 123. ... This option reads all the JSON texts in the named file and binds an array of the parsed JSON values to the given global variable.
🌐
GitHub
github.com › jqlang › jq › issues › 66
SUGGESTIONS: string manipulation as arrays; array ranges; more examples · Issue #66 · jqlang/jq
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 ...
Author   ghost
Find elsewhere
🌐
Programming Historian
programminghistorian.org › en › lessons › json-and-jq
Reshaping JSON with jq | Programming Historian
May 24, 2016 - .[].id While we know that the user ... to get the ids of every single tweet in a user’s sub-array. The command | tostring converts the tweet id numbers into strings that jq can then paste together with semicolons....
🌐
jq
jqlang.org › manual › v1.7
jq 1.7 Manual
Note that value will be treated as a string, so --arg foo 123 will bind $foo to "123". Named arguments are also available to the jq program as $ARGS.named. ... This option passes a JSON-encoded value to the jq program as a predefined variable. If you run jq with --argjson foo 123, then $foo is available in the program and has the value 123. ... This option reads all the JSON texts in the named file and binds an array of the parsed JSON values to the given global variable.
🌐
Exercism
exercism.org › tracks › jq › concepts › strings
Strings in jq on Exercism
Either index can be omitted, in which case it refers to the start or end of the string. Indexes are zero-based. "abcdefghij"[3:6] # => "def" "abcdefghij"[3:] # => "defghij" "abcdefghij"[:-2] # => "abcdefgh" ... The inverse of split/1 is join/1. Converting a string into an array of characters is frequently needed.
🌐
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....
🌐
jq
jqlang.org › manual › v1.5
jq 1.5 Manual
This is useful when using jq as a simple calculator or to construct JSON data from scratch. ... Don't parse the input as JSON. Instead, each line of text is passed to the filter as a string. If combined with --slurp, then the entire input is passed to the filter as a single long string. ... Instead of running the filter for each JSON object in the input, read the entire input stream into a large array and run the filter just once.