json - How to extract json data and output to csv using jq
问题描述
My json array file is one long file and has many objects [{},{},{},{}], within each object I need the value for three keys: "STK_NUM":"1004251 ", "DLR_COST":40.32 , "RTL_AMT":9.99. How would I write the jq program to get the key names as headers in my csv file: STK_NUM, DLR_COST,RTL_AMT and the values of these keys from all objects: 1004251,40.32,9.99 ? These three keys are in every object.
Desired csv:
STK_NUM,DLR_COST,RTL_AMT
1004251,40.32,9.99
1012658,29.99,4.69
1232556,18.89,2.49
I've tried:
jq -r .[].STK_NUM HSEitm.json
result:
"1004251"
"1012658"
"1232556"
All my other jq script attempts result in errors because I don't know what I'm doing.
If anyone could show me I would be very grateful.
解决方案
本着 DRY 的精神:
jq -r '
["STK_NUM", "DLR_COST", "RTL_AMT"] as $headers
| $headers,
# Rows:
map(.[$headers[]])
| @csv
'
推荐阅读
- suitescript - 如何通过suitescript 2.0中的UE从员工记录生成自定义记录?
- javascript - “未找到模块:错误:无法解析模块”错误在 signrequest-client 角度
- dataset - 在 Apache Jena Fuseki 中压缩数据集
- javascript - MVC - 使用 @Html.ActionLInk 触发 Ajax 以触发 ActionResult
- macos - 由于二进制文件未签名且签名不包含安全时间戳,公证 DMG 失败
- c++ - CMAKE - 如何为单个文件设置不同的编译器选项
- git - 我们如何使用 curl 和使用 ssh 密钥的凭据创建 bitbucket 存储库?
- c# - 签名未在某些特定文档上呈现
- python - Python:Django:如何根据 order_id 在 order_data 中获取 order_detail_data?
- javascript - 如何将字符串值转换为混合数组中的数字