Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 使用jq从json输出中获取键值_Linux_Bash_Shell_Curl_Jq - Fatal编程技术网

Linux 使用jq从json输出中获取键值

Linux 使用jq从json输出中获取键值,linux,bash,shell,curl,jq,Linux,Bash,Shell,Curl,Jq,我有一个如下所示的文件: { "repositories": [ { "id": "156c48fc-f208-43e8-a631-4d12deb89fa4", "namespace": "rhel12", "namespaceType": "organization", "name": "rhel6.6", "shortDescription": "", "visibility": "public" }, { "id

我有一个如下所示的文件:

{
  "repositories": [
   {
    "id": "156c48fc-f208-43e8-a631-4d12deb89fa4",
    "namespace": "rhel12",
    "namespaceType": "organization",
    "name": "rhel6.6",
    "shortDescription": "",
    "visibility": "public"
   },
   {
    "id": "f359b5d2-cb3a-4bb3-8aff-d879d51f1a04",
    "namespace": "rhel12",
    "namespaceType": "organization",
    "name": "rhel7",
    "shortDescription": "",
    "visibility": "public"
   }
  ]
 }
我只想在新行中获取每个名称值,以便在读取-r行时使用
。
我只需要

rhel6.6 
rhel7
我使用jq如下,但似乎不起作用:

jq -r '.[].name'

请在此建议正确使用jq

您需要通过
|
运算符组合过滤器:

$ jq -r '.[] | .[] | .name' test.json 
rhel6.6
rhel7
第一个
[]
获取
存储库
数组。下一个
[]
获取
存储库
数组的所有项。最后,
.name
从数组项(对象)中提取属性

注意,第一个
[]
对对象有效,因为它是一个文档化的功能:

.[]
    If you use the .[index] syntax, but omit the index entirely, it
    will return all of the elements of an array...

    You can also use this on an object, and it will return all the
    values of the object.

您希望查看存储库数组,而不是将输入视为数组:

$ jq -r '.repositories[].name' file
rhel6.6
rhel7

这是另一个解决方案。假设满足要求

我只想在新行中使用每个名称值,以便在读取-r行时使用

您能告诉我如何才能得到rhel12/rhel6.6格式的输出吗?换句话说,我需要命名空间/名称格式的o/p

如果数据位于
data.json
中,则命令

jq -M -r '.repositories[] | "\(.namespace)/\(.name)"' data.json
应该产生

rhel12/rhel6.6
rhel12/rhel7

谢谢你的回复。你能告诉我怎样才能得到格式为
rhel12/rhel6.6
的输出吗?换句话说,我需要格式为
namespace/name
的o/p。请分别发布新问题。谢谢你的回复。你能告诉我怎样才能得到格式为
rhel12/rhel6.6的输出吗?换句话说,我需要格式为
namespace/name
@meallhour,
jq-r.[].[].[.namespace,.name].[join(“/”)test.json