0

I have JSON file like the below; I need to list only x or y values only using jq:

{  
   "x":[  
      "a",
      "b",
      "c"
   ],
   "y":[  
      "d",
      "e"
   ]
}

I need to get only x values like

a
b
c

How can I do that?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116

1 Answers1

1

Easy:

cat input.json | jq '.x[]'
  • .x: get value of x property
  • []: access array elements

If you want it as a valid JSON array, remove the [] part: .x. --raw-output/-r outputs top-level strings without quotation marks, e.g.:

$ jq '.x' < input.json
[  
  "a",
  "b",
  "c"
]

$ jq '.x[]' < input.json
"a"
"b"
"c"

$ jq -r '.x[]' < input.json
a
b
c

Try it online

knittl
  • 246,190
  • 53
  • 318
  • 364