使用jq从JSON输出中提取特定字段 [英] Extract a specific field from JSON output using jq
问题描述
我的JSON输出如下:
I have a JSON output as follows:
{
"example": {
"sub-example": [
{
"name": "123-345",
"tag" : 100
},
{
"name": "234-456",
"tag" : 100
},
{
"name": "4a7-a07a5",
"tag" : 100
}
]
}
}
我想提取三个名称"字段的值并将其存储在三个变量中.
I want to extract the values of the three "name" fields and store it in three variables.
我尝试过cat json_file | jq '.["example.sub-example.name"]'
提取名称"字段的值,但这不起作用.
I tried cat json_file | jq '.["example.sub-example.name"]'
to extract the value of the "name" field but that doesn't work.
有人可以告诉我如何使用jq(或其他方法)实现这一目标吗?
Can anyone tell me how to achieve this using jq (or some other method)?
推荐答案
如果只想提取name
字段,则要查找的命令是jq '.example."sub-example" | .[] | .name'
.如果要将名称保留在数组中,请将整个jq
表达式包装在方括号中.
If you just want to extract the name
fields, the command you're looking for is jq '.example."sub-example" | .[] | .name'
. If you want to keep the names in an array, wrap the whole jq
expression in square brackets.
这篇关于使用jq从JSON输出中提取特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!