Home > Software engineering >  Parse a JSON File for values
Parse a JSON File for values

Time:09-16

I have a packer file, for which im trying to parse so i can get the specific scripts, for example in file below just want any mysql in my results e.g. install-mysql.sh, init-mysql.sh (trying to use powershell for this).

{
  "variables": {
    "image_iso": "",
    "image_sum": ""
  },
  "builders": [
    {
      "type": "virtualbox-iso",
      "vm_name": "box",
      "boot_wait": "10s",
      "disk_size": 51200,
      "guest_os_type": "RedHat_64"
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "scripts": [
        "packer/provisioners/common/base.sh",
        "packer/provisioners/common/tcp-kernel-param.sh",
        "packer/provisioners/web/create-user-webmaster.sh",
        "packer/provisioners/web/install-nginx.sh",
        "packer/provisioners/web/advanced-nginx.sh",
        "packer/provisioners/web/configure-vhost.sh",
        "packer/provisioners/mysql/mysql-kernel-param.sh",
        "packer/provisioners/mysql/install-mysql.sh",
        "packer/provisioners/mysql/init-mysql.sh",
        "packer/provisioners/redis/install-redis.sh",
        "packer/provisioners/redis/redis-kernel-param.sh",
        "packer/provisioners/misc/install-wandisco-git.sh",
        "packer/provisioners/misc/install-chrony.sh",
        "packer/provisioners/rails/install-required_package.sh",
        "packer/provisioners/rails/install-rbenv.sh",
        "packer/provisioners/common/enable-defence-portscan.sh",
        "packer/provisioners/common/vagrant.sh",
        "packer/provisioners/common/vmware.sh",
        "packer/provisioners/common/cleanup.sh"
      ],
      "override": {
        "virtualbox-iso": {
          "execute_command": "echo 'vagrant'|sudo -S sh '{{.Path}}'"
        }
      }
    },
    {
      "type": "shell",
      "script": "packer/provisioners/common/virtualbox.sh",
      "only": [
        "virtualbox-iso"
      ],
      "override": {
        "virtualbox-iso": {
          "execute_command": "echo 'vagrant'|sudo -S sh '{{.Path}}'"
        }
      }
    }
  ],
  "post-processors": [
    {
      "keep_input_artifact": false,
      "type": "vagrant",
      "output": "./packer/vagrant-boxes/CentOS-7-x86_64-Minimal.box"
    }
  ]
}

Ive currently parsed this file using powershell

$jsonfile = Get-Content -Raw .\software.json | ConvertFrom-Json

write-output $jsonfile.provisioners.scripts
packer/provisioners/common/base.sh
packer/provisioners/common/tcp-kernel-param.sh
packer/provisioners/web/create-user-webmaster.sh
packer/provisioners/web/install-nginx.sh
packer/provisioners/web/advanced-nginx.sh
packer/provisioners/web/configure-vhost.sh
packer/provisioners/mysql/mysql-kernel-param.sh
packer/provisioners/mysql/install-mysql.sh
packer/provisioners/mysql/init-mysql.sh
packer/provisioners/redis/install-redis.sh
packer/provisioners/redis/redis-kernel-param.sh
packer/provisioners/misc/install-wandisco-git.sh

Im unsure in powershell how to now parse the output above to get just the files i mentioned in the question i.e.

sqlFiles
---------------
install-mysql.sh
init-mysql.sh

CodePudding user response:

Use the Where-Object cmdlet to filter the list:

$mysqlScripts = $jsonfile.provisioners.scripts |Where-Object {$_ -match 'mysql'}

If you want to remove the leading path, use Split-Path -Leaf:

$mysqlScripts |Split-Path -Leaf
  • Related