You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在BASH环境中将Curl获取的JSON输出转换为CSV格式?

Convert JSON Array to CSV in Bash

Since you already have a JSON array stored in /tmp/1.txt, the most reliable way to convert it to valid CSV is using jq itself—it has built-in support for CSV formatting with the @csv filter, which handles edge cases like quoted strings or special characters automatically.

Method 1: Convert the existing file to CSV

Run this command to process your /tmp/1.txt file and output CSV directly:

jq -r '@csv' /tmp/1.txt

If you want to save the result to a dedicated CSV file instead of printing to stdout:

jq -r '@csv' /tmp/1.txt > /tmp/execution_result.csv

Method 2: Optimize your original workflow (one-step CSV output)

You can skip writing the intermediate JSON array to a file and generate the CSV directly from the API response. This is cleaner and eliminates extra file I/O steps:

curl -s -H "Accept: application/json" -X GET "http://192.168.253.21:4440/api/20/project/test/executions?authtoken=kH44NoX35bp1zxohgkMtsOIC9H9tw6UI" | \
jq -r '
    .executions[] 
    | select(.job.name != null and .job.name | contains("JIRA_Create_Subtask")) 
    | [.status, .job.name, ."date-started".date, ."date-ended".date, .job.project]
    | sort_by(.id) | reverse | .[0]
    | @csv' > /tmp/execution_result.csv

Why this works

The @csv filter in jq converts a JSON array into a CSV line that follows RFC4180 standards:

  • It wraps fields containing commas, quotes, or newlines in double quotes
  • It escapes any double quotes within fields by doubling them
  • Separates fields with commas correctly, avoiding accidental splits

If you absolutely need a quick-and-dirty solution with basic tools (not recommended for JSON data, as it can break with edge cases), here's a sed-based approach (only use if you're certain your fields have no commas or quotes):

sed -e 's/^\[//' -e 's/\]$//' -e 's/"//g' -e 's/, /,/g' /tmp/1.txt > /tmp/quick_and_dirty.csv

内容的提问来源于stack exchange,提问作者Milister

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:23:54