Issue
I have the following bash script:
#!/bin/bash
js="{\"fizz\":\"buzz\",\"foo\":\"baz\"}"
echo $js
json=$(echo "$js" | jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]")
echo "fetching secret json:"
echo $json
When I run it I get this:
myuser@mymachine myapp % bash myscript.sh
{"fizz":"buzz","foo":"baz"}
fetching secret json:
fizz=buzz foo=baz
In reality the JSON will be a lot larger than just 2 properties, but it will be a flat mapping of key-value pairs (no object nesting).
I actually need these map entries in comma-delimited key-value pairs. So instead of:
fizz=buzz foo=baz
I need:
fizz=buzz,foo=baz
What can I do to my jq
filters (or elsewhere) to get a comma inserted in between the key-value pairs? To be clear I'm not looking for valid JSON output, nor CSV format. I need comma-delimited KV pairs. Thanks in advance!
Solution
Just join them.
jq -r 'to_entries | map("\(.key)=\(.value)") | join(",")'
Answered By - oguz ismail Answer Checked By - Mary Flores (WPSolving Volunteer)