Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get key names from JSON file recursively using jq

I can get top-level JSON keys with jq as

jq 'keys'

E.g., see this question: Get key names from JSON file using jq

And I can enable recursion in jq:

jq '..'

As in: Recursive search values by key

But jq '.. | keys' returns jq: error at <stdin> string has no keys.

like image 822
Anton Tarasenko Avatar asked Dec 14 '22 19:12

Anton Tarasenko


1 Answers

Just ignore keys when they are absent for some values in JSON:

jq '.. | keys?'
like image 117
Anton Tarasenko Avatar answered Dec 18 '22 12:12

Anton Tarasenko