I am new to jq
, I want to convert below data:
{
"host1": "10.1.2.3" ,
"host2": "10.1.2.2" ,
"host3": "10.1.18.1"
}
to this below format:
host1 : 10.1.2.3
host2 : 10.1.2.2
host3 : 10.1.18.1
An alternate version, not present in the potentially duplicate question linked in the comments.
jq -r 'keys_unsorted[] as $k | [ $k, .[$k] ] | join(" : ")'
Store the keys in $k
and get the value associated with it, and put the results to an array [..]
and join the array elements by :
Convert the object to an association list with to_entries
, then construct the desired string using interpolation. The -r
option produces raw text, rather than JSON-encoded strings.
$ jq -r 'to_entries[] | "\(.key) : \(.value)"' tmp.json
host1 : 10.1.2.3
host2 : 10.1.2.2
host3 : 10.1.18.1