jq to_entries string and number cannot be added

Viewed 1915

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?

1 Answers

what am I missing?

A pair of parentheses:

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

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

"\(.key)=\(.value)"
Related