Concat 2 fields in JSON using jq

Concat 2 fields in JSON using jq

Use parentheses around the string concatenation code:

echo {channel: youtube, profile_type: video, member_key: hello} 
 | jq {channel: (.profile_type + . + .channel)}

Here is a solution that uses string interpolation as Jeff suggested:

{channel: (.profile_type).(.member_key)}

e.g.

$ jq {channel: (.profile_type).(.member_key)} <<EOF
> {channel: youtube, profile_type: video, member_key: hello}
> EOF
{
  channel: video.hello
}

String interpolation works with the (foo) syntax (which is similar to a shell $(foo) call).
See the official JQ manual.

Concat 2 fields in JSON using jq

Leave a Reply

Your email address will not be published. Required fields are marked *