Steven Fowler
Steven Fowler

Reputation: 95

JQ: How to add literal value to output

Thank you for looking at this with me.

I am using JQ to manipulate JSON files.

Started with this that works:

jq ".[]|{name:.name,type:.type}" r.json

Need to extent to include "acct" and a literal value of "acct1" into the resulting json.

I know I can do it with two lines of code

jq '.[]+{acct:"acct1"}' r.json > r2.json
jq "{acct:.acct,name:.name,type:.type}" r2.json > r.json

Is there a way to do it inline with one line of code - like this which is not working ?

jq '.[]|{acct:"acct1",name:.name,type:.type}' r.json

Thank you for looking at this problem

Upvotes: 6

Views: 9626

Answers (2)

peak
peak

Reputation: 116740

A more economical approach to achieving what I understand you want would be:

jq '.[]|{name,type, acct:"acct1"}' r.json

Incidentally, the first filter in the Q (.[]|{name:.name,type:.type}) can be abbreviated to:

.[]|{name,type}

Upvotes: 1

Steven Fowler
Steven Fowler

Reputation: 95

The answer I was looking for was

jq '.[]+{acct:"acct1"}|{acct:.acct,name:.name,type:.type}" r2.json > r.json

Just had to learn how |'s worked :)

Upvotes: 0

Related Questions