0

I want to convert the below content from JSON to CSV with the keys as headers thru jq. Please help.

{
  "BackupJobs": [
    {
      "AccountId": "12345678",
      "BackupJobId": "asd8f6wehrnvas-cker2-dfne",
      "BackupVaultName": "aws/efs/automatic-backup-vault",
      "BackupVaultArn": "arn:aws:backup:ha-middleeast-1:12345678:backup-vault:aws/efs/automatic-backup-vault",
      "RecoveryPointArn": "arn:aws:backup:ha-middleeast-1:12345678:recovery-point:asd8f6wehrnvas-cker2-dfne",
      "ResourceArn": "arn:aws:elasticfilesystem:ha-middleeast-1:12345678:file-system/fs-47jsg3y47",
      "CreationDate": "2023-12-31T10:30:00+05:30",
      "CompletionDate": "2023-01-09T11:56:40.074000+05:30",
      "State": "COMPLETED",
      "PercentDone": "100.0",
      "BackupSizeInBytes": 0,
      "IamRoleArn": "arn:aws:iam::12345678:role/aws-service-role/backup.amazonaws.com/backuprole",
      "CreatedBy": {
        "BackupPlanId": "aws/efs/asd8f6wehrnvas-cker2-dfne",
        "BackupPlanArn": "arn:aws:backup:ha-middleeast-1:12345678:backup-plan:aws/efs/asd8f6wehrnvas-cker2-dfne",
        "BackupPlanVersion": "asd8f6wehrnvas-cker2-dfne",
        "BackupRuleId": "asd8f6wehrnvas-cker2-dfne"
      },
      "StartBy": "2023-01-09T18:30:00+05:30",
      "ResourceType": "EFS",
      "IsParent": false
    },
    {
      "AccountId": "12345678",
      "BackupJobId": "asd8f6wehrnvas-cker2-dfne",
      "BackupVaultName": "aws/efs/automatic-backup-vault",
      "BackupVaultArn": "arn:aws:backup:ha-middleeast-1:12345678:backup-vault:aws/efs/automatic-backup-vault",
      "RecoveryPointArn": "arn:aws:backup:ha-middleeast-1:12345678:recovery-point:asd8f6wehrnvas-cker2-dfne",
      "ResourceArn": "arn:aws:elasticfilesystem:ha-middleeast-1:12345678:file-system/fs-47jsg3y47",
      "CreationDate": "2023-12-31T10:30:00+05:30",
      "CompletionDate": "2023-01-09T11:56:40.074000+05:30",
      "State": "COMPLETED",
      "PercentDone": "100.0",
      "BackupSizeInBytes": 0,
      "IamRoleArn": "arn:aws:iam::12345678:role/aws-service-role/backup.amazonaws.com/backuprole",
      "CreatedBy": {
        "BackupPlanId": "aws/efs/asd8f6wehrnvas-cker2-dfne",
        "BackupPlanArn": "arn:aws:backup:ha-middleeast-1:12345678:backup-plan:aws/efs/asd8f6wehrnvas-cker2-dfne",
        "BackupPlanVersion": "asd8f6wehrnvas-cker2-dfne",
        "BackupRuleId": "asd8f6wehrnvas-cker2-dfne"
      },
      "StartBy": "2023-01-09T18:30:00+05:30",
      "ResourceType": "EFS",
      "IsParent": false
    }
  ]
}

The below command works to convert JSON to CSV of the values. Cant figure out to put the keys as headers.

aws backup list-backup-jobs --output json |
jq -r '.BackupJobs | map([.AccountId,.BackupJobId,.BackupVaultName,.RecoveryPointArn,.ResourceArn,.CreationDate,.CompletionDate,.State,.PercentDone,.BackupSizeInBytes,.ResourceType] 
                   | join(", ")) 
                   | join("\n")'

Keys are AccountId, BackupJobId, BackupVaultName......

brian d foy
  • 129,424
  • 31
  • 207
  • 592
learner
  • 1
  • 1
  • 3
    In short: `["Your", "headers", "here"], (.BackupJobs[] | [.your, .values, .here]) | @csv`. In detail: [How to convert arbitrary simple JSON to CSV using jq?](https://stackoverflow.com/questions/32960857/how-to-convert-arbitrary-simple-json-to-csv-using-jq) – pmf Jan 09 '23 at 08:11
  • Why don't you just use [miller](https://miller.readthedocs.io/en/latest/file-formats/)? It handles both JSON and CSV natively. – jena Jan 09 '23 at 13:52

0 Answers0