0

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
Inian
  • 71,145
  • 9
  • 121
  • 139
Nikhil
  • 91
  • 2
  • 11

2 Answers2

2

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 :

Inian
  • 71,145
  • 9
  • 121
  • 139
1

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
chepner
  • 446,329
  • 63
  • 468
  • 610