需要帮助修复Shell脚本以将字符串转换为JSON响应。

huangapple go评论60阅读模式
英文:

Need help to fix shell script to convert string to json response

问题

I am trying to convert below string which is stored inside a txt file into json formatted string

High Severity Results: 0
Medium Severity Results: 1
Low Severity Results: 2
Information Severity Results: 0
Scan Results Location: Https://www.checkmarx.abc.com/cxwebclient/viewermain.aspx?scanid=12345&projectid=1234

to below json format

{
"High Severity Results" : "0",
"Medium Severity Results" : "1",
"Low Severity Results" : "2",
"Information Severity Results" : "0",
"Scan Results Location" : "Https://www.checkmarx.abc.com/cxwebclient/viewermain.aspx?scanid=12345&projectid=1234",
}

I tried the below command.

file="Checkmarx_Json_Data.txt";
checkmarxDataNew="$(cat Checkmarx_Json_Data.txt)";
while IFS= read -r line || [[ -n "$line" ]]; do
  for val in $line
  do
    echo "\"${val//:/\":\"}\"" >> ACM_BQ_JSON_Response.json
  done
done < "$file"

but the above script is generating response in below format

"checkmarx" : {
"High"
"Severity"
"Results":""0""
"Medium"
"Severity"
"Results":""1""
"Low"
"Severity"
"Results":""2""
"Information"
"Severity"
"Results":""0""
"Scan"
"Results"
"Location":"""
"Https":"//www.checkmarx.ford.com/cxwebclient/viewermain.aspx?scanid=2500093&amp;projectid=28069"
}

when I tried with a Single string like color:red, it worked well.

can someone help me to resolve this issue.

英文:

I am trying to convert below string which is stored inside a txt file into json formatted string

High Severity Results: 0
Medium Severity Results: 1
Low Severity Results: 2
Information Severity Results: 0
Scan Results Location: Https://www.checkmarx.abc.com/cxwebclient/viewermain.aspx?scanid=12345&amp;projectid=1234

to below json format

{
&quot;High Severity Results&quot; : &quot;0&quot;,
&quot;Medium Severity Results&quot; : &quot;1&quot;,
&quot;Low Severity Results&quot; : &quot;2&quot;,
&quot;Information Severity Results&quot; : &quot;0&quot;,
&quot;Scan Results Location&quot; : &quot;Https://www.checkmarx.abc.com/cxwebclient/viewermain.aspx?scanid=12345&amp;projectid=1234&quot;,
}

I tried the below command.

file=&quot;Checkmarx_Json_Data.txt&quot;
checkmarxDataNew=&quot;$(cat Checkmarx_Json_Data.txt)&quot;
while IFS= read -r line || [[ -n &quot;$line&quot; ]]; do
  for val in $line
  do
    echo &quot;\&quot;${val//:/\&quot;:\&quot;}\&quot;&quot; &gt;&gt; ACM_BQ_JSON_Response.json
  done
done &lt; &quot;$file&quot;

but the above script is generating response in below format

&quot;checkmarx&quot; : {
&quot;High&quot;
&quot;Severity&quot;
&quot;Results&quot;:&quot;&quot;
&quot;0&quot;
&quot;Medium&quot;
&quot;Severity&quot;
&quot;Results&quot;:&quot;&quot;
&quot;1&quot;
&quot;Low&quot;
&quot;Severity&quot;
&quot;Results&quot;:&quot;&quot;
&quot;2&quot;
&quot;Information&quot;
&quot;Severity&quot;
&quot;Results&quot;:&quot;&quot;
&quot;0&quot;
&quot;Scan&quot;
&quot;Results&quot;
&quot;Location&quot;:&quot;&quot;
&quot;Https&quot;:&quot;//www.checkmarx.ford.com/cxwebclient/viewermain.aspx?scanid=2500093&amp;projectid=28069&quot;
}

when I tried with a Single string like color:red, it worked well.

can someone help me to resolve this issue.

答案1

得分: 1

以下是使用 jq 完成的方式:

jq --raw-input 'split("\\s*:\\s*"; "") | 
  { (.[0]): (.[1:] | join(":")) }' < input-file | \
jq --slurp add

输出结果:

{
  "High Severity Results": "0",
  "Medium Severity Results": "1",
  "Low Severity Results": "2",
  "Information Severity Results": "0",
  "Scan Results Location": "Https://www.checkmarx.abc.com/cxwebclient/viewermain.aspx?scanid=12345&amp;projectid=1234"
}
英文:

Here's how it can be done with jq:

jq --raw-input &#39;split(&quot;\\s*:\\s*&quot;; &quot;&quot;) | 
  { (.[0]): (.[1:] | join(&quot;:&quot;)) }&#39; &lt; input-file | \
jq --slurp add

output:

{
  &quot;High Severity Results&quot;: &quot;0&quot;,
  &quot;Medium Severity Results&quot;: &quot;1&quot;,
  &quot;Low Severity Results&quot;: &quot;2&quot;,
  &quot;Information Severity Results&quot;: &quot;0&quot;,
  &quot;Scan Results Location&quot;: &quot;Https://www.checkmarx.abc.com/cxwebclient/viewermain.aspx?scanid=12345&amp;projectid=1234&quot;
}

答案2

得分: 0

使用 sed

$ sed -Ee 's/$/,/;1i{' -e '$a}' -e 's/^([^:]*)(: )([^,]*)/"" ""/' Checkmarx_Json_Data.txt
{
"High Severity Results" : "0",
"Medium Severity Results" : "1",
"Low Severity Results" : "2",
"Information Severity Results" : "0",
"Scan Results Location" : "Https://www.checkmarx.abc.com/cxwebclient/viewermain.aspx?scanid=12345&projectid=1234",
}
英文:

Using sed

$ sed -Ee &#39;s/$/,/;1i\{&#39; -e &#39;$a\}&#39; -e &#39;s/^([^:]*)(: )([^,]*)/&quot;&quot; &quot;&quot;/&#39; Checkmarx_Json_Data.txt
{
&quot;High Severity Results&quot; : &quot;0&quot;,
&quot;Medium Severity Results&quot; : &quot;1&quot;,
&quot;Low Severity Results&quot; : &quot;2&quot;,
&quot;Information Severity Results&quot; : &quot;0&quot;,
&quot;Scan Results Location&quot; : &quot;Https://www.checkmarx.abc.com/cxwebclient/viewermain.aspx?scanid=12345&amp;projectid=1234&quot;,
}

答案3

得分: 0

#!/usr/bin/env bash

total_cnt=$(wc -l < Checkmarx_Json_Data.txt)

{
printf {
while IFS= read -r line; do
  col1=${line%%:*}
  col2=${line#*: }
  if ((++line_cnt < total_cnt)); then
    printf '  "%s" : "%s",\n' "$col1" "$col2"
  else
    printf '  "%s" : "%s"\n' "$col1" "$col2"
  fi
done < Checkmarx_Json_Data.txt
printf }
} > ACM_BQ_JSON_Response.json

If ed is available/acceptable, something like:

The script.ed (name it to your own hearts content)

,s/\([^:]*\)\(: \)\(.*\)/  "" ""/
1,$-1s/$/,/
0a
{
.
$a
}
.
,p
Q

Now run:

ed -s Checkmarx_Json_Data.txt < script.ed

To create a new file with the edited output,

ed -s Checkmarx_Json_Data.txt < script.ed > ACM_BQ_JSON_Response.json
英文:
#!/usr/bin/env bash

total_cnt=$(wc -l &lt; Checkmarx_Json_Data.txt)

{
printf {\\n
while IFS= read -r line; do
  col1=${line%%:*}
  col2=${line#*: }
  if ((++line_cnt &lt; total_cnt)); then
    printf &#39;  &quot;%s&quot; : &quot;%s&quot;,\n&#39; &quot;$col1&quot; &quot;$col2&quot;
  else
    printf &#39;  &quot;%s&quot; : &quot;%s&quot;\n&#39; &quot;$col1&quot; &quot;$col2&quot;
  fi
done &lt; Checkmarx_Json_Data.txt
printf }
} &gt; ACM_BQ_JSON_Response.json

If ed is available/acceptable, something like:

The script.ed (name it to your own hearts content)

,s/\([^:]*\)\(: \)\(.*\)/  &quot;&quot; &quot;&quot;/
1,$-1s/$/,/
0a
{
.
$a
}
.
,p
Q

Now run:

ed -s Checkmarx_Json_Data.txt &lt; script.ed

To create a new file with the edited output,

ed -s Checkmarx_Json_Data.txt &lt; script.ed &gt; ACM_BQ_JSON_Response.json

huangapple
  • 本文由 发表于 2023年3月3日 23:05:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定