Bobby Wan-Kenobi
Bobby Wan-Kenobi

Reputation: 915

Parsing JSON: Stuck in a seemingly easy jq task

I have a JSON of this sort:

{
  "key1": {
      "unicode": "f26e",
      "label": "pink",
      "free": false
  },
  "key2": {
      "unicode": "b1e3",
      "label": "red",
      "free": true
  },
  "key3": {
      "unicode": "11a2",
      "label": "blue",
      "free": false
  }
  ...
}

And I want to extract the pair label/unicode from each inner object. So far I only could get the list of all keys with:

jp '. | keys' icons.json

Do I have to start drilling from there?

Upvotes: 0

Views: 187

Answers (1)

peak
peak

Reputation: 116640

The following:

.[] | {unicode,"label": .label}

produces:

{"unicode":"f26e","label":"pink"}
{"unicode":"b1e3","label":"red"}
{"unicode":"11a2","label":"blue"}

The "gotcha" here is that "label" is a keyword, and so one cannot write {label} as an abbreviation for {"label":.label}.

Upvotes: 1

Related Questions