简体   繁体   English

使用 jq 列出 JSON 文件的键值

[英]List key values of JSON file using jq

I have JSON file like the below;我有 JSON 文件,如下所示; I need to list only x or y values only using jq:我只需要使用 jq 列出 x 或 y 值:

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

I need to get only x values like我只需要获取 x 值,例如

a
b
c

How can I do that?我怎样才能做到这一点?

Easy:简单的:

cat input.json | jq '.x[]'
  • .x : get value of x property .x : 获取x属性的值
  • [] : access array elements [] : 访问数组元素

If you want it as a valid JSON array, remove the [] part: .x .如果您希望它作为有效的 JSON 数组,请删除[]部分: .x --raw-output / -r outputs top-level strings without quotation marks, eg: --raw-output / -r输出不带引号的顶级字符串,例如:

$ 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在线尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM