Behrang Saeedzadeh
Behrang Saeedzadeh

Reputation: 47971

How to map a list of key/values to a space separated list of keyValue=valueValue entries in jq?

How can I transform this output:

$ jq -s '.[0] * .[1] * .[2] | to_entries' a.json b.json c.json
[
  {
    "key" : "SimpleNumber",
    "value" : "123"
  },
  {
    "key" : "SimpleString",
    "value" : "Hello"
  },
  {
    "key" : "ComplexString",
    "value" : "Hello World"
  }
]

to:

1)

"SimpleNumber=123" "SimpleString=Hello" "ComplexString=Hello World"

and

2)

SimpleNumber="123" SimpleString="Hello" ComplexString="Hello World"

Upvotes: 0

Views: 120

Answers (2)

0stone0
0stone0

Reputation: 44182

String interpolation combined with join():

jq --raw-output 'map("\"\(join("="))\"") | join(" ")'

to generate

"SimpleNumber=123" "SimpleString=Hello" "ComplexString=Hello World"

Try it online


Or to it 'by hand' to quote only the value:

jq --raw-output 'map("\(.key)=\"\(.value)\"") | join(" ")'
SimpleNumber="123" SimpleString="Hello" ComplexString="Hello World"

Try it online

Upvotes: 1

pmf
pmf

Reputation: 36391

Use string interpolation and put the quotes as you see fit:

  1. around whole items
jq -r 'map("\"\(.key)=\(.value)\"") | join(" ")'
"SimpleNumber=123" "SimpleString=Hello" "ComplexString=Hello World"

Demo

  1. just around the "value"
jq -r 'map("\(.key)=\"\(.value)\"") | join(" ")'
SimpleNumber="123" SimpleString="Hello" ComplexString="Hello World"

Demo

Upvotes: 1

Related Questions