我有一个json文件,它存储在环境变量temp。
中 {
"users": [
{
"username": "jack",
"email": "jack@somewhere.com",
"total running apps": "1",
"api-mock-app": "0",
"flogo": "1",
"ipaas": "0",
"nodejs-app": "0"
},
{
"username": "jill",
"email": "jill@somewhere.com",
"total running apps": "1",
"api-mock-app": "0",
"flogo": "1",
"ipaas": "0",
"nodejs-app": "0"
}
]
}
当我使用此命令将此JSON转换为CSV时
jq -r '.users[] | [.username, .email, .total running apps, .api-mock-app, .flogo, .ipaas, .nodejs-app] | @csv' <<< $temp
我收到了以下错误。
jq:错误:语法错误,意外IDENT(Unix shell引用问题?)at第1行:
.users [] | [.username,.email,.total正在运行的应用,.api-mock-app,.flogo,.ipaas,.nodejs-app] | @csv
jq:1编译错误
请帮忙吗?
答案 0 :(得分:0)
您需要引用列名,特别是因为它们包含空格和短划线。但是你需要转义bash的引号:
jq --raw-output '.users[] | [.username, .email, ."total running apps", ."api-mock-app", ."flogo", ."ipaas", ."nodejs-app"] | @csv'
答案 1 :(得分:0)
我得到了这个有效的代码click here
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<html>
<head>
<title>Demo - Covnert JSON to CSV</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="https://github.com/douglascrockford/JSON-js/raw/master/json2.js"></script>
<script type="text/javascript">
// JSON to CSV Converter
function ConvertToCSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ','
line += array[i][index];
}
str += line + '\r\n';
}
return str;
}
// Example
$(document).ready(function () {
// Create Object
var items = [
{ name: "Item 1", color: "Green", size: "X-Large" },
{ name: "Item 2", color: "Green", size: "X-Large" },
{ name: "Item 3", color: "Green", size: "X-Large" }];
// Convert Object to JSON
var jsonObject = JSON.stringify(items);
// Display JSON
$('#json').text(jsonObject);
// Convert JSON to CSV & Display CSV
$('#csv').text(ConvertToCSV(jsonObject));
});
</script>
</head>
<body>
<h1>
JSON</h1>
<pre id="json"></pre>
<h1>
CSV</h1>
<pre id="csv"></pre>
</body>
</html>
&#13;