我是 JQ 的新手,我有以下 json 文件
[
{
"userId" : "jens",
"firstName" : "jens",
"lastName" : "jens",
"emailAddress" : "[email protected]",
"source" : "default",
"status" : "active",
"readOnly" : false,
"roles" : [ "reader" ],
"externalRoles" : [ ]
}, {
"userId" : "admin",
"firstName" : "Administrator",
"lastName" : "User",
"emailAddress" : "[email protected]",
"source" : "default",
"status" : "changepassword",
"readOnly" : false,
"roles" : [ "app-admin" ],
"externalRoles" : [ ]
}]
现在我想按以下格式打印
"userId" : "jens","roles" : [ "reader" ]
"userId" : "admin", "roles" : [ "app-admin" ],
我怎样才能做到这一点?我试过这个命令
jq -r '.[].userId,.[].roles'
但它像下面一样分散数据
jens
admin
[]
[
"reader",
"app-admins",
]
请指导我如何获得上述输出。
如果你这样做
你会得到
该
-c
标志会将每个对象放在自己的行上,因此只要您没有嵌套对象,这应该可以满足您的要求。后处理,例如摆脱大括号:生产