gpt4 book ai didi

json - 在 bash 脚本中添加 JSON 元素

转载 作者:可可西里 更新时间:2023-11-01 17:04:48 27 4
gpt4 key购买 nike

在我的 bash 脚本中有一行看起来像这样:

JOB_RESULTS=$(curl --fail -k -H "Content-Type: application/xml" --write-out HTTP_CODE='%{http_code}' "${request_url}")

这一行输出如下:

{"name":"callCLF010Job","id":11693,"status":"STARTING","message":"The job has started.","exitCode":"exitCode=UNKNOWN;exitDescription="}HTTP_CODE=200

问题是,我想将 HTTP_CODE 添加到 JSON 消息中。

有没有办法让返回看起来像 {"name":"callCLF010Job","id":11693,"status":"STARTING","message":"The job has started.","exitCode":"exitCode=UNKNOWN;exitDescription=","HTTP_CODE":"200"}

编辑:

随着五彩纸屑的变化,我的代码如下所示:

http_code="${JOB_RESULTS:${#JOB_RESULTS}-17}"
http_body="${JOB_RESULTS:0:${#JOB_RESULTS}-17}"
http_code_json=", ${http_code}}"
my_result="${http_body/%\}/$http_code_json}"

当我运行 echo $my_result 时,我的输出如下所示:

{"name":"callCLF010Job","id":11702,"status":"STARTING","message":"The job has started.","exitCode":"exitCode=UNKNOWN;exitDescriptio

最佳答案

一种选择是使用 sed 以您想要的方式格式化输出:

echo $JOB_RESULTS | sed 's/}HTTP_CODE=\([0-9]\+\)$/,"HTTP_CODE":"\1"}/'

或者在你的命令中:

JOB_RESULTS=$(curl --fail -k -H "Content-Type: application/xml" --write-out HTTP_CODE='%{http_code}' "${request_url}" | sed 's/}HTTP_CODE=\([0-9]\+\)$/,"HTTP_CODE":"\1"}/')

关于json - 在 bash 脚本中添加 JSON 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52298846/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com