如何用jq及基础Linux命令将非标准CSV转为指定结构JSON?
非标准CSV转指定JSON的jq实现方案
问题背景
现有一份非标准CSV文件,格式如下:
Asserts Samples 1;expecting command finishes successfully (bash exe);7ms 1;expecting command finishes successfully (loading bin, execute);27ms 0;expecting command fails (bash exe, execute);23ms ... 0;expecting command prints some message (bash exe);12ms 0;expecting command prints some message (loading bin, execute);26ms 5;2;71.43;136ms
需要转换为指定结构的JSON:
{ "testName": "Asserts Samples", "tests": [ { "name": "expecting command finishes successfully (bash exe)", "status": false, "duration": "7ms" }, ... { "name": "expecting command prints some message (loading bin, execute)", "status": true, "duration": "26ms" } ], "summary": { "success": 5, "failed": 2, "rating": 71.43, "duration": "136ms" } }
实现步骤
利用基础Linux命令(head、tail)拆分非标准行,结合jq完成结构组装和字段转换:
完整命令
将各部分数据提取与JSON结构组装整合为一条命令:
jq -n \ --arg testName "$(head -n 1 input.csv)" \ --argjson tests "$(head -n -1 input.csv | tail -n +2 | jq -R 'split(";") | {name: .[1], status: .[0] == "0", duration: .[2]}')" \ --arg summaryLine "$(tail -n 1 input.csv)" \ '{ testName: $testName, tests: $tests, summary: ($summaryLine | split(";") | { success: .[0] | tonumber, failed: .[1] | tonumber, rating: .[2] | tonumber, duration: .[3] }) }'
关键细节说明
- 非标准行拆分:用
head -n -1去掉末行、tail -n +2去掉首行,精准提取中间测试用例行 - status字段转换:直接通过
. [0] == "0"生成布尔值,替代复杂的select操作,更简洁高效 - 数值类型处理:汇总数据中的
success、failed、rating用tonumber转为数字类型,符合JSON规范 - 变量传递:用
--arg传递字符串类型的测试名称,--argjson传递已处理的测试用例JSON数组,确保结构拼接正确
内容的提问来源于stack exchange,提问作者Vanger69
相关产品推荐
相关产品推荐

