[英]Json to csv using shell script
這是我的 json
[
{
"shortname":"aarti",
"customer_name":"aarti dhebe",
"supportedEquipments":{
"domains":" home_appliance, commercial_kitchen ",
"equipment":"refrigerator,washer"
},
"data":{
"dc":{
"tag":[
"generated",
"approved "
],
"public":true,
"private":true
}
}
},{
"shortname":"rohan",
"customer_name":"rohan patil",
"supportedEquipments":{
"domains":" home_appliance, commercial_kitchen ",
"equipment":"refrigerator,washer"
},
"data":{
"dc":{
"tag":[
"generated",
"approved "
],
"public":true,
"private":false
}
}
},{
"shortname":"laxmi",
"customer_name":"laxmi sakhre",
"supportedEquipments":{
"domains":" home_appliance, commercial_kitchen ",
"equipment":"refrigerator,washer"
},
"data":{
"dc":{
"tag":[
"generated",
"approved "
],
"public":false,
"private":true
}
}
},{
"shortname":"aarushi",
"customer_name":"aarushi rasali",
"supportedEquipments":{
"domains":" home_appliance, commercial_kitchen ",
"equipment":"refrigerator,washer"
},
"data":{
"dc":{
"tag":[
"generated",
"approved "
],
"public":false,
"private":false
}
}
}
]
我想將 json 文件轉換為具有設備類型的 csv。我應該根據設備類型列生成 csv。 洗衣機分開csv,冰箱分開。 所以我寫了代碼。
#!/bin/bash
# Read the JSON file
json=$(cat input.json)
# Remove extra whitespace from the JSON file
json=$(echo $json | sed -e 's/[[:space:]]*$//' -e 's/^[[:space:]]*//')
# Extract the values for each customer
while read -r line; do
shortname=$(echo $line | jq -r '.shortname')
customer_name=$(echo $line | jq -r '.customer_name')
domains=$(echo $line | jq -r '.supportedEquipments.domains')
equipment=$(echo $line | jq -r '.supportedEquipments.equipment')
tags=$(echo $line | jq -r '.data.dc.tag | join("|")')
public=$(echo $line | jq -r '.data.dc.public')
private=$(echo $line | jq -r '.data.dc.private')
# Determine the value for the Availability field
if [ "$public" = "true" ] && [ "$private" = "true" ]; then
availability="global|$shortname"
elif [ "$public" = "true" ]; then
availability="global"
elif [ "$private" = "true" ]; then
availability="$shortname"
else
availability="global"
fi
# Set the value for the Organization field
organization="abc"
# Create the CSV file
uuid=$(uuidgen)
filename="${uuid}_${shortname}__${equipment}.csv"
echo "AccessDetails,AccessRights,Action,DocumentCategory,DocumentContentType,D omain,EquipmentSubCategory,EquipmentType,Id,Manufacturer,ModelNumbers,Organizati on,Resource,Summary,Tags,Title,Availability,CustomerShortname" > $filename
echo ", , , , ,$domains, ,$equipment, , , ,$organization, , ,$tags, ,$availabi lity,$shortname" >> $filename
# Create separate CSV files for refrigerator and washer
if [[ "$equipment" == *"refrigerator"* ]]; then
cp $filename "${uuid}_refrigerator.csv"
elif [[ "$equipment" == *"washer"* ]]; then
cp $filename "${uuid}_washer.csv"
fi
done <<< "$(echo $json | jq -c '.[]')"
這是我的,得到了預期 output 它應該看起來像這樣 [在此處輸入圖片描述]( https://i.stack.imgur.com/T6SLq.png )
我能夠生成 csv 但它沒有被分配到它的領域。 它隨機填充 csv 上的數據。
請幫助我,我不明白我在哪里犯了錯誤。 請
我想將文件另存為 .csv
你的任務可以用jq完全解決:
#!/bin/bash
jq -r --arg organization "abc" '
def trim:
sub("^[[:space:]]+"; "") | sub("[[:space:]]+$"; "");
map([.data.dc.public as $public |
.data.dc.private as $private |
(.shortname | trim) as $shortname |
( if $public and $private then "global|\($shortname)"
elif $public then "global"
elif $private then .shortname
else "global"
end
) as $availability |
"", "", "", "", "",
(.supportedEquipments.domains | trim), "",
(.supportedEquipments.equipment | trim), "", "", "",
$organization, "", "",
(.data.dc.tag | map(trim) | join("|")), "",
($availability | trim),
$shortname
]) |
["AccessDetails", "AccessRights", "Action", "DocumentCategory", "DocumentContentType", "Domain", "EquipmentSubCategory", "EquipmentType", "Id", "Manufacturer", "ModelNumbers", "Organization", "Resource", "Summary", "Tags", "Title", "Availability", "CustomerShortname"],
.[] |
@csv
' input.json
Output
"AccessDetails","AccessRights","Action","DocumentCategory","DocumentContentType","Domain","EquipmentSubCategory","EquipmentType","Id","Manufacturer","ModelNumbers","Organization","Resource","Summary","Tags","Title","Availability","CustomerShortname"
"","","","","","home_appliance, commercial_kitchen","","refrigerator,washer","","","","abc","","","generated|approved","","global|aarti","aarti"
"","","","","","home_appliance, commercial_kitchen","","refrigerator,washer","","","","abc","","","generated|approved","","global","rohan"
"","","","","","home_appliance, commercial_kitchen","","refrigerator,washer","","","","abc","","","generated|approved","","laxmi","laxmi"
"","","","","","home_appliance, commercial_kitchen","","refrigerator,washer","","","","abc","","","generated|approved","","global","aarushi"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.