如何用jq将JSON参数文件解析为分号分隔的字符串
用jq解析JSON为指定键值对格式
输入JSON
[ { "ParameterKey": "code", "ParameterValue": "ab" }, { "ParameterKey": "env", "ParameterValue": "prod" }, { "ParameterKey": "api", "ParameterValue": "testapi" }, { "ParameterKey": "name", "ParameterValue": "new" } ]
解决命令
直接使用以下jq命令即可得到目标格式输出:
jq -r 'map("\(.ParameterKey)=\(.ParameterValue)") | join(";")' input.json
命令解释
map("\(.ParameterKey)=\(.ParameterValue)"):遍历数组中的每个对象,将每个对象的ParameterKey和ParameterValue拼接成key=value格式的字符串,最终生成一个字符串数组。join(";"):将上述字符串数组用分号;连接成单个完整字符串。-r:指定jq输出原始字符串(不带JSON格式的引号),确保输出符合需求。
执行结果
运行命令后会输出:
code=ab;env=prod;api=testapi;name=new
内容的提问来源于stack exchange,提问作者user24285544
相关产品推荐
相关产品推荐

