wxzmsewu
wxzmsewu

Reputation: 1

Exclude JSON array fields for specifc values

Let's say I want to extract a CSV taking specific fields from a big json file generated from an API... everything of interest is inside an array.

jq -r ".array[] | [.uniqV,.V1,.V2,.V3] | @csv" something.json

... but if the unique value starts with a certain string (say, BAD-), I want to exclude the array member entirely. I could do something like this...

jq -r ".array[] | [.uniqV,.v1,.v2,.v3] | @csv" something.json | egrep -v ^\"BAD-

... but it would be nicer / more efficient to exclude this pattern within jq itself. How could that be accomplished?

Upvotes: 0

Views: 175

Answers (1)

chepner
chepner

Reputation: 532418

Filter the records from .array before building the array fed to @csv.

jq -r '.array[] | 
         select(.uniqV | startswith("BAD-") | not) |
         [.uniqV, .v1, .v2, .v3] |
         @csv' something.json

Upvotes: 1

Related Questions