简体   繁体   中英

Json to csv using shell script

this is my 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
         }
      }
   }

]

i want to convert json file to csv with equipment type.I should generate csv ason to the equipment type column. Separate csv for washer and separate for refrigerator. So i have written the code.

#!/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 '.[]')"

this is my and got getting expected output it should look like this [ enter image description here ]( https://i.stack.imgur.com/T6SLq.png )

i am able generate csv but it's not getting assinged to it's field. It's fill the data on csv randomly.

please help me I'm not able understand where I'm making mistake. please

and i want save file as.csv


Your task can be completely solved with 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM