Home > database >  Strings to json in bash
Strings to json in bash

Time:08-24

I've got the following string as a output from for-loop:

something1/value1 something2/value2

Now I need to convert it to json. Expected output:

["something1/value1","something2/value2"]

I tried this:

echo $var | jq -R -s -c 'split("\n")[:-1]'

But the result is ugly

["something1/value1\rsomething2/value2"].

How do I properly make json from my string?

CodePudding user response:

with jq you just have to do

echo $var | jq -R -c 'split(" ")'

  • Related