После преобразования json в yaml поле «null» добавляется во все поля моих значений. - PullRequest
0 голосов
/ 02 мая 2020

Ниже приведен словарь, который я использую для преобразования в json.

    string = {resources:{name:vm-created-by-deployment-manager,type:compute.v1.instance,properties : 
           {zone:us- 
         central1-a,machinetype:zones/us-central1-a/machineTypes/n1-standard-1,disk: 
         {deviceName:boot,type:PERSISTENT,boot:true ,autoDelete:true ,initializeParams: 
         {sourceImage:projects/debian-cloud/global/images/family/debian-9}},networkInterfaces: 
         {network:global/networks/default}}}}

Здесь я пытаюсь преобразовать данные dict в json

    r = json.dumps(string)
    loaded_r = json.loads(r)

Здесь я я пытаюсь преобразовать json data ymal и записать в файл yaml.

with open("C:\\Users\\Jeevitha\\Desktop\\DM_test.yaml", 'w') as f:
      yaml.dump(yaml.load(loaded_r,yaml.FullLoader), f,default_flow_style=False,sort_keys=False)

вывести то, что я получил.

    resources:
      name:vm-created-by-deployment-manager: null
  type:compute.v1.instance: null
  properties:
     zone:us-central1-a: null
     machinetype:zones/us-central1-a/machineTypes/n1-standard-1: null
     disk:
       deviceName:boot: null
       type:PERSISTENT: null
       boot:true: null
       autoDelete:true: null
       initializeParams:
           sourceImage:projects/debian-cloud/global/images/family/debian-9: null
     networkInterfaces:
        network:global/networks/default: null

вывести то, что мне нужно.

      resources:
         name: vm-created-by-deployment-manager
         type: compute.v1.instance
         properties:
             zone: us-central1-a
             machineType: zones/us-central1-a/machineTypes/n1-standard-1
             disks:
                deviceName: boot
                type: PERSISTENT
                boot: true
                autoDelete: true
                initializeParams:
                    sourceImage: projects/debian-cloud/global/images/family/debian-9
             networkInterfaces:
                 network: global/networks/default
...