Join an array to create JSON dynamically

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Declaring JSON in bash is kind of annoying because you have to escape a lot of characters.
Say I have an array like this:
value1="foo"
value2="bar"
arr=("key1" "$value1" "key2" "$value2")
Is there a way to somehow join the array with ":" and "," characters.
The only thing I can think of is a loop where you add the right characters, something like this:
data="";
for i in "$arr[@]"; do
data="$data"$i""
done
bash shell-script shell
add a comment |Â
up vote
2
down vote
favorite
Declaring JSON in bash is kind of annoying because you have to escape a lot of characters.
Say I have an array like this:
value1="foo"
value2="bar"
arr=("key1" "$value1" "key2" "$value2")
Is there a way to somehow join the array with ":" and "," characters.
The only thing I can think of is a loop where you add the right characters, something like this:
data="";
for i in "$arr[@]"; do
data="$data"$i""
done
bash shell-script shell
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Declaring JSON in bash is kind of annoying because you have to escape a lot of characters.
Say I have an array like this:
value1="foo"
value2="bar"
arr=("key1" "$value1" "key2" "$value2")
Is there a way to somehow join the array with ":" and "," characters.
The only thing I can think of is a loop where you add the right characters, something like this:
data="";
for i in "$arr[@]"; do
data="$data"$i""
done
bash shell-script shell
Declaring JSON in bash is kind of annoying because you have to escape a lot of characters.
Say I have an array like this:
value1="foo"
value2="bar"
arr=("key1" "$value1" "key2" "$value2")
Is there a way to somehow join the array with ":" and "," characters.
The only thing I can think of is a loop where you add the right characters, something like this:
data="";
for i in "$arr[@]"; do
data="$data"$i""
done
bash shell-script shell
edited Apr 22 at 2:40
slmâ¦
235k65480653
235k65480653
asked Apr 21 at 0:22
Alexander Mills
1,885929
1,885929
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Assuming the array is correct:
echo '['
printf '"%s": "%s",n' "$arr[@]" | sed '$s/,$//'
echo ']'
The sed command removes the comma from the end of the last line.
Then pass this through jq to have it formatted correctly:
sed '$s/,$//'
echo ']'
| jq .
With the given data, this produces
[
"key1": "foo"
,
"key2": "bar"
]
This will obviously treat all values as strings, which is what they are in the shell as well.
yeah, I realized you can declare dynamic JSON using heredoc in bash, that seems to be the easiest way, albeit a little bit ugly.
â Alexander Mills
Apr 21 at 8:14
add a comment |Â
up vote
1
down vote
Turns out heredoc might be the best way to declare dynamic JSON, but I didn't know about that technique when developed the following.
This is one solution, use it like so:
join_arry_to_json a b c d
"a":"b","c":"d"
this will only work for strings, not for numbers or booleans.
Now to declare a boolean or a number, you use the ^ symbol:
ql_join_arry_to_json a ^3 b ^true c dog
yields:
"a":3,"b":true,"c":"dog"
the code is here:
https://gist.github.com/ORESoftware/a4e3948b0ce9c22752c759d7e694c9ab
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Assuming the array is correct:
echo '['
printf '"%s": "%s",n' "$arr[@]" | sed '$s/,$//'
echo ']'
The sed command removes the comma from the end of the last line.
Then pass this through jq to have it formatted correctly:
sed '$s/,$//'
echo ']'
| jq .
With the given data, this produces
[
"key1": "foo"
,
"key2": "bar"
]
This will obviously treat all values as strings, which is what they are in the shell as well.
yeah, I realized you can declare dynamic JSON using heredoc in bash, that seems to be the easiest way, albeit a little bit ugly.
â Alexander Mills
Apr 21 at 8:14
add a comment |Â
up vote
1
down vote
accepted
Assuming the array is correct:
echo '['
printf '"%s": "%s",n' "$arr[@]" | sed '$s/,$//'
echo ']'
The sed command removes the comma from the end of the last line.
Then pass this through jq to have it formatted correctly:
sed '$s/,$//'
echo ']'
| jq .
With the given data, this produces
[
"key1": "foo"
,
"key2": "bar"
]
This will obviously treat all values as strings, which is what they are in the shell as well.
yeah, I realized you can declare dynamic JSON using heredoc in bash, that seems to be the easiest way, albeit a little bit ugly.
â Alexander Mills
Apr 21 at 8:14
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Assuming the array is correct:
echo '['
printf '"%s": "%s",n' "$arr[@]" | sed '$s/,$//'
echo ']'
The sed command removes the comma from the end of the last line.
Then pass this through jq to have it formatted correctly:
sed '$s/,$//'
echo ']'
| jq .
With the given data, this produces
[
"key1": "foo"
,
"key2": "bar"
]
This will obviously treat all values as strings, which is what they are in the shell as well.
Assuming the array is correct:
echo '['
printf '"%s": "%s",n' "$arr[@]" | sed '$s/,$//'
echo ']'
The sed command removes the comma from the end of the last line.
Then pass this through jq to have it formatted correctly:
sed '$s/,$//'
echo ']'
| jq .
With the given data, this produces
[
"key1": "foo"
,
"key2": "bar"
]
This will obviously treat all values as strings, which is what they are in the shell as well.
answered Apr 21 at 7:59
Kusalananda
102k13199315
102k13199315
yeah, I realized you can declare dynamic JSON using heredoc in bash, that seems to be the easiest way, albeit a little bit ugly.
â Alexander Mills
Apr 21 at 8:14
add a comment |Â
yeah, I realized you can declare dynamic JSON using heredoc in bash, that seems to be the easiest way, albeit a little bit ugly.
â Alexander Mills
Apr 21 at 8:14
yeah, I realized you can declare dynamic JSON using heredoc in bash, that seems to be the easiest way, albeit a little bit ugly.
â Alexander Mills
Apr 21 at 8:14
yeah, I realized you can declare dynamic JSON using heredoc in bash, that seems to be the easiest way, albeit a little bit ugly.
â Alexander Mills
Apr 21 at 8:14
add a comment |Â
up vote
1
down vote
Turns out heredoc might be the best way to declare dynamic JSON, but I didn't know about that technique when developed the following.
This is one solution, use it like so:
join_arry_to_json a b c d
"a":"b","c":"d"
this will only work for strings, not for numbers or booleans.
Now to declare a boolean or a number, you use the ^ symbol:
ql_join_arry_to_json a ^3 b ^true c dog
yields:
"a":3,"b":true,"c":"dog"
the code is here:
https://gist.github.com/ORESoftware/a4e3948b0ce9c22752c759d7e694c9ab
add a comment |Â
up vote
1
down vote
Turns out heredoc might be the best way to declare dynamic JSON, but I didn't know about that technique when developed the following.
This is one solution, use it like so:
join_arry_to_json a b c d
"a":"b","c":"d"
this will only work for strings, not for numbers or booleans.
Now to declare a boolean or a number, you use the ^ symbol:
ql_join_arry_to_json a ^3 b ^true c dog
yields:
"a":3,"b":true,"c":"dog"
the code is here:
https://gist.github.com/ORESoftware/a4e3948b0ce9c22752c759d7e694c9ab
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Turns out heredoc might be the best way to declare dynamic JSON, but I didn't know about that technique when developed the following.
This is one solution, use it like so:
join_arry_to_json a b c d
"a":"b","c":"d"
this will only work for strings, not for numbers or booleans.
Now to declare a boolean or a number, you use the ^ symbol:
ql_join_arry_to_json a ^3 b ^true c dog
yields:
"a":3,"b":true,"c":"dog"
the code is here:
https://gist.github.com/ORESoftware/a4e3948b0ce9c22752c759d7e694c9ab
Turns out heredoc might be the best way to declare dynamic JSON, but I didn't know about that technique when developed the following.
This is one solution, use it like so:
join_arry_to_json a b c d
"a":"b","c":"d"
this will only work for strings, not for numbers or booleans.
Now to declare a boolean or a number, you use the ^ symbol:
ql_join_arry_to_json a ^3 b ^true c dog
yields:
"a":3,"b":true,"c":"dog"
the code is here:
https://gist.github.com/ORESoftware/a4e3948b0ce9c22752c759d7e694c9ab
edited Apr 22 at 6:49
answered Apr 21 at 0:32
Alexander Mills
1,885929
1,885929
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f439046%2fjoin-an-array-to-create-json-dynamically%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password