将 json 文件转换为 linux jq 命令格式的 csv

问题描述 投票:0回答:1

我想将 csv 中的 json 文件转换为我们想要的格式。 我只想从 json 中提取特定变量并将其格式化为 csv。

当前的 json 文件

{
      "id": 33,
      "type": "organization",
      "url": "/api/v2/organizations/33/",
      "related": {
        "projects": "/api/v2/organizations/33/projects/",
        "inventories": "/api/v2/organizations/33/inventories/",
        "job_templates": "/api/v2/organizations/33/job_templates/",
        "workflow_job_templates": "/api/v2/organizations/33/workflow_job_templates/",
        "users": "/api/v2/organizations/33/users/",
        "admins": "/api/v2/organizations/33/admins/",
        "teams": "/api/v2/organizations/33/teams/",
        "credentials": "/api/v2/organizations/33/credentials/",
        "applications": "/api/v2/organizations/33/applications/",
        "activity_stream": "/api/v2/organizations/33/activity_stream/",
        "notification_templates": "/api/v2/organizations/33/notification_templates/",
        "notification_templates_started": "/api/v2/organizations/33/notification_templates_started/",
        "notification_templates_success": "/api/v2/organizations/33/notification_templates_success/",
        "notification_templates_error": "/api/v2/organizations/33/notification_templates_error/",
        "notification_templates_approvals": "/api/v2/organizations/33/notification_templates_approvals/",
        "object_roles": "/api/v2/organizations/33/object_roles/",
        "access_list": "/api/v2/organizations/33/access_list/",
        "instance_groups": "/api/v2/organizations/33/instance_groups/",
        "galaxy_credentials": "/api/v2/organizations/33/galaxy_credentials/"
      },
      "summary_fields": {
        "object_roles": {
          "admin_role": {
            "description": "Can manage all aspects of the organization",
            "name": "Admin",
            "id": 25340,
            "user_only": true
          },
          "execute_role": {
            "description": "May run any executable resources in the organization",
            "name": "Execute",
            "id": 25341
          },
          "project_admin_role": {
            "description": "Can manage all projects of the organization",
            "name": "Project Admin",
            "id": 25342
          },
          "inventory_admin_role": {
            "description": "Can manage all inventories of the organization",
            "name": "Inventory Admin",
            "id": 25343
          },
          "credential_admin_role": {
            "description": "Can manage all credentials of the organization",
            "name": "Credential Admin",
            "id": 25344
          },
          "workflow_admin_role": {
            "description": "Can manage all workflows of the organization",
            "name": "Workflow Admin",
            "id": 25345
          },
          "notification_admin_role": {
            "description": "Can manage all notifications of the organization",
            "name": "Notification Admin",
            "id": 25346
          },
          "job_template_admin_role": {
            "description": "Can manage all job templates of the organization",
            "name": "Job Template Admin",
            "id": 25347
          },
          "auditor_role": {
            "description": "Can view all aspects of the organization",
            "name": "Auditor",
            "id": 25348
          },
          "member_role": {
            "description": "User is a member of the organization",
            "name": "Member",
            "id": 25349,
            "user_only": true
          },
          "read_role": {
            "description": "May view settings for the organization",
            "name": "Read",
            "id": 25350
          },
          "approval_role": {
            "description": "Can approve or deny a workflow approval node",
            "name": "Approve",
            "id": 25351
          }
        },
        "user_capabilities": {
          "edit": true,
          "delete": true
        },
        "related_field_counts": {
          "inventories": 1,
          "teams": 0,
          "users": 4,
          "job_templates": 5,
          "admins": 4,
          "projects": 5
        }
      },
      "created": "2021-04-22T09:49:38.652168Z",
      "modified": "2021-04-22T09:49:38.652182Z",
      "name": "VDI-Team",
      "description": "",
      "max_hosts": 0,
      "custom_virtualenv": null
    },

我要提取并格式化为 csv 的字段是 “ID” “类型” “related_field_counts” “姓名” “描述”

如何使用 linux jq 命令提取这些?

arrays json csv jq export-to-csv
1个回答
0
投票

要将这些字段提取到 csv,您可以在 shell 脚本中使用

jq
@csv
过滤器,如下所示:

#!/bin/bash


json=$(cat input.json)

fields=$(echo "$json" | jq -r '.[] | [
    .id,
    .type,
    .summary_fields.related_field_counts.inventories,
    .summary_fields.related_field_counts.teams,
    .summary_fields.related_field_counts.users,
    .summary_fields.related_field_counts.job_templates,
    .summary_fields.related_field_counts.admins,
    .summary_fields.related_field_counts.projects,
    .name,
    .description
] | @csv')


echo "id,type,inventories,teams,users,job_templates,admins,projects,name,description" > output.csv
echo "$fields" >> output.csv
© www.soinside.com 2019 - 2024. All rights reserved.