jq 传递一个应转换为 json 数组的数组

tot*_*tot 1 arrays bash shell json jq

我有脚本

#!/bin/bash
ARR=("a" "b")

collection_init_msg=$( jq -n --arg arr $ARR '{arr: [$arr]}')

echo some command "$collection_init_msg" 
Run Code Online (Sandbox Code Playgroud)

应该转换 ARR 并将其打印为 JSON 数组。

当前结果

一些命令 { "arr": [ "a" ] }

我想要的是:

某些命令 { "arr": [ "a", "b" ] }

pea*_*eak 5

#!/bin/bash
ARR=("a" "b")

collection_init_msg=$( jq -nc '{arr: $ARGS.positional}'  --args "${ARR[@]}"  )

echo "$collection_init_msg" 
Run Code Online (Sandbox Code Playgroud)

回答评论中的补充问题:可以做得比以下更糟糕:

jq -n --argjson a1 $(jq -nc '$ARGS.positional' --args "${A1[@]}") '
  {$a1, a2: $ARGS.positional}' --args "${A2[@]}"
Run Code Online (Sandbox Code Playgroud)