我已经研究过并试了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。
谢谢!