How to format a bash array as a JSON array

后端 未结 5 932
醉酒成梦
醉酒成梦 2021-01-02 05:02

I have a bash array

X=(\"hello world\" \"goodnight moon\")

That I want to turn into a json array

[\"hello world\", \"goodni         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 05:28

    You can use:

    X=("hello world" "goodnight moon")
    sed 's/^/[/; s/,$/]/' <(printf '"%s",' "${X[@]}") | jq -s '.'
    [
      [
        "hello world",
        "goodnight moon"
      ]
    ]
    

提交回复
热议问题