-1

I've some string inputted by user such as:

read -p "define module tags (example: TAG1, TAG2): " -r module_tags
if [ "$module tags" = "" ]; then module tags="TAG1, TAG2"; fi

which are tags separated by ,

Than, I need to append these tags in a JSON array field:

{
    "modules": [
        {
            "tags": "<user-custom-tags>"
        }
    ]
}

I would do it in this way:

args='.modules[0].tags = '$module_tags''

tmp=$(mktemp -u)

jq --indent 4 "$args" $plugin_file > "$tmp" && mv "$tmp" $plugin_file

But for this, I need to transform the input TAG1, TAG2 to [ "TAG1", "TAG2" ]

How would you do this?

markzzz
  • 47,390
  • 120
  • 299
  • 507

3 Answers3

0
for tag in $module_tags_array

This splits the value of $module_tags_array on IFS, i.e. you are looping over first TAG1, and then TAG2, not the list of tags.

What you are describing can easily be accomplished with

module_tags="[ $module_tags_array ]"

Notice also that the proper way to echo the value of the variable is with quotes:

echo "$module_tags"

unless you specifically require the shell to perform whitespace tokenization and wildcard expansion on the unquoted value. See also When to wrap quotes around a shell variable?

However, a more natural and obvious solution is to actually use an array to store the values.

tags=("TAG1" "TAG2")
printf "\"%s\", " "${tags[@]}" | sed 's/^/[/;s/, $/]/'

The printf produces a string like

"TAG1", "TAG2",

which we then massage into a JSON array expression with a quick bit of sed postprocessing.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • `module_tags="[ $module_tags ]"` won't work. I'm appending (later, with jq) this array to a JSON. Each item need to be "TAG", and not TAG – markzzz Aug 27 '19 at 08:20
  • So your actual question is how to add double quotes around the values, too? That's not actually obvious from your question. – tripleee Aug 27 '19 at 08:21
  • 1
    @markzzz: State your entire question pipeline, right from where you get these inputs from and how _exactly_ would you like to transform it – Inian Aug 27 '19 at 08:24
  • what if I want to store the result on a var instead of printf? `myVar="${tags[@]}" | sed 's/^/[/;s/, $/]/'` does nothing... – markzzz Aug 27 '19 at 08:32
  • You really need to read up on shell syntax. See also `printf -v`. Heaping on new requirements in comments is not really acceptable -- search and if you still need help ask a new question with all your requirements stated up front. – tripleee Aug 27 '19 at 08:33
  • I've updated the question. Can you make a new answer? Or do I need a new question? – markzzz Aug 27 '19 at 08:49
  • Replacing your question after you have received answers is not really acceptable. But too late now to go back I guess. In the future, just accept one of the answers (post one of your own if you like) and ask a new question. – tripleee Aug 27 '19 at 09:06
  • Created a new question so: https://stackoverflow.com/questions/57672838/how-to-add-double-quotes-around-the-values-separated-by-a-delimiter – markzzz Aug 27 '19 at 10:37
0

Not using bashism, but using jq command line JSON parser:

<<< "TAG1, TAG2" jq -Rc 'split(", ")'
["TAG1","TAG2"]

-R is for raw input string
-c is for compact JSON output (on 1 line)
As expected, split function turns the input string into different part and put them into a JSON array.

oliv
  • 12,690
  • 25
  • 45
  • how can "store" the result of that jq in a var? That just "print" to the terminal. I need to store the result... – markzzz Aug 27 '19 at 09:00
  • use: `my_var=$(<<< "TAG1, TAG2" jq -Rc 'split(", ")')` – oliv Aug 27 '19 at 09:02
  • oliv the same of ceving solution: it doesn't work with `TAG1,TAG2` (i..e no space after `,`). How can I fix it? It should works the same for both cases – markzzz Aug 27 '19 at 09:08
  • use: `split(", ?")` to add the optional space after the `,` – oliv Aug 27 '19 at 09:10
  • Tried `module_tags=$(<<< $module_tags jq -Rc 'split(", ?")')`, but its the same, doesn't work – markzzz Aug 27 '19 at 09:12
  • please quote your bash variable `"$module_tags"` – oliv Aug 27 '19 at 09:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/198517/discussion-between-markzzz-and-oliv). – markzzz Aug 27 '19 at 09:47
0

Use the "Search and Replace" feature of Bash's parameter expansion:

input="TAG1, TAG2"
output='[ "'${input//, /\", \"}'" ]'
printf "%s\n" "$output"

But be aware that this is not a proper way to quote.

ceving
  • 21,900
  • 13
  • 104
  • 178