4

I'm not understanding how to_entries works in jq.

I have the following json payload in payload.json

{"REGION":"us-east-1","EMAIL":"contain","UPDATE":1}

which I want to convert into = delimited keypairs, like so;

REGION=us-east-1
EMAIL=contain
UPDATE=1

I was using

jq -r 'to_entries | .[] | .key + "=" + .value' < payload.json

But I get an error

jq: error (at <stdin>:0): string ("UPDATE=") and number (1) cannot be added

If I understand correctly, the issue is that the update value is a number, not a string (ie, having them not match types is an issue) so I tried the following, both with the same error;

string interpolation:
jq -r 'to_entries | .[] | (.key) + "=" + (.value)' < payload.json

tostring:
jq -r 'to_entries | .[] | .key + "=" + .value|tostring' < payload.json

What am I missing?

Alex
  • 2,555
  • 6
  • 30
  • 48
  • possible duplicate of https://stackoverflow.com/questions/25378013/how-to-convert-a-json-object-to-key-value-format-in-jq – Rmahajan Jan 14 '19 at 15:29

1 Answers1

6

what am I missing?

A pair of parentheses:

.key + "=" + ( .value|tostring )

Alternatively, you could use string interpolation, e.g.

"\(.key)=\(.value)"
peak
  • 105,803
  • 17
  • 152
  • 177
  • It never occured to me to combine them, but yep works like a charm. I'll accept as soon as I can, thanks! – Alex Jan 14 '19 at 15:24