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
  • 80,270
  • 14
  • 142
  • 161
Nikhil
  • 101
  • 2
  • 13

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
  • 80,270
  • 14
  • 142
  • 161
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
  • 497,756
  • 71
  • 530
  • 681