英文:
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&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&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&projectid=28069"
}
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&projectid=1234"
}
英文:
Here's how it can be done with jq
:
jq --raw-input 'split("\\s*:\\s*"; "") |
{ (.[0]): (.[1:] | join(":")) }' < input-file | \
jq --slurp add
output:
{
"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"
}
答案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 '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",
}
答案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 < Checkmarx_Json_Data.txt)
{
printf {\\n
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论