jq:转换为CSV时编辑单个字段

时间:2016-04-13 12:54:16

标签: json csv jq

简介

我已经研究过并试了2天。我自己找不到类似问题或找到答案都没有成功。

我有以下JSON有效负载(来自VRA API)

{ "content": [
 {
  "@type": "CatalogResource",
  "id": "ccc",
  "iconId": "xxx",
  "resourceTypeRef": {
    "id": "Infrastructure.Virtual",
    "label": "Virtual Machine"
 },
  "name": "name01",
  "description": "example01",
  "status": "ACTIVE",
  "catalogItem": {
    "id": "xxxxx",
    "label": "xxxxx"
 },
  "requestId": "xxxxx",
  "providerBinding": {
    "bindingId": "xxxx",
    "providerRef": {
      "id": "xxxxx",
      "label": "xxxxx"
    }
 },
  "owners": [
    {
      "tenantName": "xxxxx",
      "ref": "xxxxxxxx",
      "type": "USER",
      "value": "xxxxxxxxx"
    }
  ],
  "organization": {
    "tenantRef": "xxxx",
    "tenantLabel": "xxxxxxx",
    "subtenantRef": "xxxxxx",
    "subtenantLabel": "xxxxxxxxx"
 },
  "dateCreated": "2015-10-05T08:58:35.133Z",
  "lastUpdated": "2015-12-03T13:23:54.187Z",
  "hasLease": true,
  "lease": {
    "start": "2015-10-05T08:21:31.000Z"
 },
  "leaseForDisplay": null,
  "hasCosts": true,
  "costs": {
    "leaseRate": {
      "type": "moneyTimeRate",
      "cost": {
        "type": "money",
        "currencyCode": "GBP",
        "amount": 99999
      },
      "basis": {
        "type": "timeSpan",
        "unit": "DAYS",
        "amount": 1
      }
    }
 },
  "costToDate": {
    "type": "money",
    "currencyCode": "GBP",
    "amount": 19181
 },
  "totalCost": null,
  "childResources": [],
  "operations": null,
  "forms": {
    "catalogResourceInfoHidden": true,
    "details": {
      "type": "extension",
      "extensionId": "xxxxxx",
      "extensionPointId": null
    }
  },
  "resourceData": {
    "entries": []
  }
},
{
  "@type": "CatalogResource",
  "id": "ccc",
  "iconId": "xxx",
  "resourceTypeRef": {
    "id": "Infrastructure.Virtual",
    "label": "Virtual Machine"
  },
  "name": "name01",
  "description": "this, is, my, problem",
  "status": "ACTIVE",
  "catalogItem": {
    "id": "xxxxx",
    "label": "xxxxx"
  },
  "requestId": "xxxxx",
  "providerBinding": {
    "bindingId": "xxxx",
    "providerRef": {
      "id": "xxxxx",
      "label": "xxxxx"
    }
  },
  "owners": [
    {
      "tenantName": "xxxxx",
      "ref": "xxxxxxxx",
      "type": "USER",
      "value": "xxxxxxxxx"
    }
  ],
  "organization": {
    "tenantRef": "xxxx",
    "tenantLabel": "xxxxxxx",
    "subtenantRef": "xxxxxx",
    "subtenantLabel": "xxxxxxxxx"
  },
  "dateCreated": "2015-10-05T08:58:35.133Z",
  "lastUpdated": "2015-12-03T13:23:54.187Z",
  "hasLease": true,
  "lease": {
    "start": "2015-10-05T08:21:31.000Z"
  },
  "leaseForDisplay": null,
  "hasCosts": true,
  "costs": {
    "leaseRate": {
      "type": "moneyTimeRate",
      "cost": {
        "type": "money",
        "currencyCode": "GBP",
        "amount": 99999
      },
      "basis": {
        "type": "timeSpan",
        "unit": "DAYS",
        "amount": 1
      }
    }
  },
  "costToDate": {
    "type": "money",
    "currencyCode": "GBP",
    "amount": 19181
  },
  "totalCost": null,
  "childResources": [],
  "operations": null,
  "forms": {
    "catalogResourceInfoHidden": true,
    "details": {
      "type": "extension",
      "extensionId": "xxxxxx",
      "extensionPointId": null
    }
  },
  "resourceData": {
    "entries": []
  }
}
]
}

我将其转换为CSV,如下所示:

jq --raw-output -r '.content[0] | [.name,.id,.resourceTypeRef.label,.description,.status,.catalogItem.label,.owners[0].value,.dateCreated,.costs.leaseRate.cost.amount,.costToDate.amount] | @csv'

问题

我需要编辑.description字段并删除逗号。

我正在寻找 jq 的方法来做到这一点;我可以使用 sed awk 在shell级别执行此操作,但我想知道是否可以在我用于生成CSV的同一命令中使用jq。

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用gsub/2使用正则表达式执行替换。因此,对于描述部分,请进行替换。

(.description | gsub(","; ""))