以下は私のJSONコードです
[
{
"kind": "user",
"version": "v2",
"metadata": {
"name": "person1"
},
"spec": {
"roles": [
"access",
"auditor",
"editor",
"abc-access"
],
"status": {
"is_locked": false,
"locked_time": "0001-01-01T00:00:00Z",
"lock_expires": "0001-01-01T00:00:00Z",
"recovery_attempt_lock_expires": "0001-01-01T00:00:00Z"
},
"expires": "0001-01-01T00:00:00Z",
"created_by": {
"time": "2023-06-07T08:50:58.559675944Z",
"user": {
"name": "person2"
}
}
}
},
{
"kind": "user",
"version": "v2",
"metadata": {
"name": "person2"
},
"spec": {
"roles": [
"access",
"auditor",
"editor",
"abc-access"
],
"status": {
"is_locked": false,
"locked_time": "0001-01-01T00:00:00Z",
"lock_expires": "0001-01-01T00:00:00Z",
"recovery_attempt_lock_expires": "0001-01-01T00:00:00Z"
},
"expires": "0001-01-01T00:00:00Z",
"created_by": {
"time": "2023-06-06T14:01:15.256905162Z",
"user": {
"name": "person5"
}
}
}
},
{
"kind": "user",
"version": "v2",
"metadata": {
"name": "person3"
},
"spec": {
"roles": [
"access",
"auditor",
"editor",
"abc-access"
],
"status": {
"is_locked": false,
"locked_time": "0001-01-01T00:00:00Z",
"lock_expires": "0001-01-01T00:00:00Z",
"recovery_attempt_lock_expires": "0001-01-01T00:00:00Z"
},
"expires": "0001-01-01T00:00:00Z",
"created_by": {
"time": "2023-06-15T12:16:09.35013254Z",
"user": {
"name": "person2"
}
}
}
},
{
"kind": "user",
"version": "v2",
"metadata": {
"name": "person3"
},
"spec": {
"roles": [
"access",
"auditor",
"editor",
"abc-access"
],
"status": {
"is_locked": false,
"locked_time": "0001-01-01T00:00:00Z",
"lock_expires": "0001-01-01T00:00:00Z",
"recovery_attempt_lock_expires": "0001-01-01T00:00:00Z"
},
"expires": "0001-01-01T00:00:00Z",
"created_by": {
"time": "2023-06-06T13:45:30.543520308Z",
"user": {
"name": "person6"
}
}
}
},
{
"kind": "user",
"version": "v2",
"metadata": {
"name": "person4"
},
"spec": {
"roles": [
"access",
"auditor",
"editor",
"abc-access"
],
"status": {
"is_locked": false,
"locked_time": "0001-01-01T00:00:00Z",
"lock_expires": "0001-01-01T00:00:00Z",
"recovery_attempt_lock_expires": "0001-01-01T00:00:00Z"
},
"expires": "0001-01-01T00:00:00Z",
"created_by": {
"time": "2023-06-07T04:49:41.39245724Z",
"user": {
"name": "person5"
}
}
}
},
{
"kind": "user",
"version": "v2",
"metadata": {
"name": "person5"
},
"spec": {
"roles": [
"access",
"auditor",
"editor",
"abc-access"
],
"traits": {
"aws_role_arns": null,
"db_names": null,
"db_users": null,
"kubernetes_groups": null,
"kubernetes_users": null,
"logins": [
"ec2-user",
"root"
],
"windows_logins": null
},
"status": {
"is_locked": false,
"locked_time": "0001-01-01T00:00:00Z",
"lock_expires": "0001-01-01T00:00:00Z",
"recovery_attempt_lock_expires": "0001-01-01T00:00:00Z"
},
"expires": "0001-01-01T00:00:00Z",
"created_by": {
"time": "2023-06-06T13:51:39.173360152Z",
"user": {
"name": "person6"
}
}
}
},
{
"kind": "user",
"version": "v2",
"metadata": {
"name": "person6"
},
"spec": {
"roles": [
"access",
"editor",
"auditor"
],
"traits": {
"aws_role_arns": null,
"azure_identities": null,
"db_names": null,
"db_roles": null,
"db_users": null,
"gcp_service_accounts": null,
"kubernetes_groups": null,
"kubernetes_users": null,
"logins": [
"root",
"ubuntu",
"ec2-user"
],
"windows_logins": null
},
"status": {
"is_locked": false,
"locked_time": "0001-01-01T00:00:00Z",
"lock_expires": "0001-01-01T00:00:00Z",
"recovery_attempt_lock_expires": "0001-01-01T00:00:00Z"
},
"expires": "0001-01-01T00:00:00Z",
"created_by": {
"time": "2023-06-06T13:41:41.579010534Z",
"user": {
"name": "xyz"
}
}
}
}
]
csvに変換します
jq -r '.[] | [ .metadata.name ] , [ .spec.roles[] ] | @csv' ind.json
上記のコマンドの出力は次のとおりです。
"person1"
"alpha","gamma","beta","abc-access"
"person2"
"alpha","gamma","beta","abc-access"
"person3"
"alpha","gamma","beta","abc-access"
"person3"
"alpha","gamma","beta","abc-access"
"person4"
"alpha","gamma","beta","abc-access"
"person5"
"alpha","gamma","beta","abc-access"
"person6"
"alpha","gamma","beta","auditor"
別の列に名前と役割を割り当てたいです。名前と役割が同じ行にあり、十分なスペースがあるためにExcelにコピーして貼り付けると、2つの列になりたいです。 1つは「名前」を含み、もう1つは「役割」を含みます。二重引用符なしで出力を取得する場合は、オプションも見つけます。
以下は期待される出力です。
person1 alpha,gamma,beta,abc-access
person2 alpha,gamma,beta,abc-access
person3 alpha,gamma,beta,abc-access
person3 alpha,gamma,beta,abc-access
person4 alpha,gamma,beta,abc-access
person5 alpha,gamma,beta,abc-access
person6 alpha,gamma,beta,auditor
答え1
2番目の列は、単純なカンマで区切られた文字列のリストである2つのタブで区切られた列が必要であるとします。
$ jq -r '["name","roles"], map([.metadata.name, (.spec.roles|join(","))])[] | @tsv' file
name roles
person1 access,auditor,editor,abc-access
person2 access,auditor,editor,abc-access
person3 access,auditor,editor,abc-access
person3 access,auditor,editor,abc-access
person4 access,auditor,editor,abc-access
person5 access,auditor,editor,abc-access
person6 access,editor,auditor
このjq
式は、2要素配列セットを渡し、@tvs
タブ区切りの出力形式で指定します。最初の配列は静的ヘッダーで、残りは呼び出しから来ますmap()
。このmap()
呼び出しは構造体から名前を抽出し、構造体の役割metadata
文字列から単純なカンマ区切り文字列を生成しますspec
。