将 csv 从新列中的 json 转换出来

将 csv 从新列中的 json 转换出来

下面是我的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 中复制粘贴,它将是两列,一列包含“名称”,另一列包含“角色”。如果我们得到不带双引号的输出,还要寻找选项

以下是预期输出

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

我假设您需要两个制表符分隔的列,其中第二列是简单的逗号分隔的字符串列表。

$ 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

相关内容