Antoine Gallix
Antoine Gallix

Reputation: 816

Shorter construct for filtering object keys in json data using jq

My data is a list of objects with many keys. To explore the data, I only want to look at a restricted set of keys.

input:
[
    {
        "field_1":123,
        "field_2":123,
        "field_3":123,
        "field_4":123,
        "field_5":123,
        ...and so on...
    },
    {
        "field_1":123,
        "field_2":123,
        "field_3":123,
        "field_4":123,
        "field_5":123,
        ...and so on...
    },
    ...many objects like this...
]
I want a result like:
[
    {
        "field_1":123,
        "field_2":123,
    },
    {
        "field_1":123,
        "field_2":123,
    },
    ...all objects like this...
]

I know I can do:

jq '.[] | {field_1:.field_1,field_2:.field_2}'

But it gets repetitive for data exploration on the command line. Is there a shortcut function available like:

jq '.[] | filter_keys("field_1","field_2")'

Upvotes: 1

Views: 186

Answers (2)

Jeff Mercado
Jeff Mercado

Reputation: 134571

Depending on your situation, you might want to only include fields that exist in the objects so you'll have to do some path filtering. You could then just pass in a list of fields to be included. Or even do pattern based filtering.

$ jq --arg fields 'field_1,field_2' '
map(with_entries(select(. as {$key} | any($fields|split(",")[]; $key == .))))
' input.json

And as with any filter, they can be made into functions and saved to your ~/.jq file to be globally available with something like this:

def filter_keys($keys):
    with_entries(select(. as {$key} | any($keys[]; $key == .)));

And using it:

$ jq --arg fields 'field_1,field_2' 'map(filter_keys($fields|split(",")))'

Upvotes: 1

Antoine Gallix
Antoine Gallix

Reputation: 816

There is a shorthand notation for that:

jq '.[] | {field_1,field_2}'

Upvotes: 2

Related Questions