Saturday, July 9, 2022

[SOLVED] JQ - get keys without values in a simple list

Issue

I have a Pipfile.lock JSON file that I need to parse with the jq tool. The structure of the file is:

{
     //...
     "default": {
           "value1": {
           // lots of nested properties with values
           },
           "value2": {
           //similar stuff as for value1
           }
           //... 
     }
}

What do now is jq '.default | keys' Pipfile.lock which produces:

[
    "value1",
    "value2",
...
]

I would need the output to:

  1. Not produce the first line with [ and last line with ]
  2. Have the values printed without quotes

Is this feasible just using jq?


Solution

You want the elements of the array, not the array itself.

jq '.default | keys[]' Pipfile.lock

Use the -r option to output raw strings, rather than JSON strings.

jq -r '.default | keys[]' Pipfile.lock


Answered By - chepner
Answer Checked By - Marie Seifert (WPSolving Admin)