1

I have parsed the content of an API call to a variable $a.(Content below) and I would like to only parse out the list of packages which are under "dependencies". Is there are way to filter for only the dependencies using powershell?.

{
"name": "1package",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
    "@babel/runtime": {
        "version": "7",
        "resolved": "https://registry.npmjs.org",
        "integrity": "***",
        "requires": {
            "regenerator-runtime": "^0.13.4"
        }
    },
    "@cloud": {
        "version": "2",
        "resolved": "https://registry.npmjs.org/",
        "integrity": "***"
    },
    "@cloudnative/health-connect": {
        "version": "2",
        "resolved": "https://registry.npmjs.org/@***.tgz",
        "integrity": "***",
        "requires": {
            "@cloudnative/health": "^2.1.1"
        }
    },

So I just want to parse out a list having

babel/runtime version 7
cloud version 2
cloudnative/health-connect version 2
  • 1
    I'd recommend, if it's a JSON, let's treat it as such: `.. | ConvertFrom-Json | Select-Object -ExpandProperty Dependencies`. – Abraham Zinala Jan 19 '22 at 23:56
  • Thanks alot. exactly what I wanted. it returns something like @actions/core : @{version=1.2.6; resolved=https://registry.npmjs.org/@actions/core/-/core-1.2.6.tgz; integrity=sha512-ZQYitnqiyBc3D+k7LsgSBmMDVkOVidaagDG7j3fOym77jNunWRuYx7VSHa9GNfFZh+zh61xsCjRj4JxMZlDqTA==}. what if I want it to return only the "version" and "resolved" property for each dependency. Thanks alot – Caleb Adepoju Jan 20 '22 at 00:17

1 Answers1

3

By accessing the PSObject.Properties of each object we can get the dependency "Name" and the desired values of the properties "Version" and "Resolved". Using a calculated property with Select-Object we can construct a new object.

Note, this code assumes you have already used ConvertFrom-Json over your Json string and, the object is stored in the $json variable.

$json.dependencies.PSObject.Properties | Select-Object Name,
    @{
        Name = 'Version'
        Expression = { $_.Value.Version }
    }, @{
        Name = 'Resolved'
        Expression = { $_.Value.Resolved }
    }

Output

Name                        Version Resolved
----                        ------- --------
@babel/runtime              7       https://registry.npmjs.org
@cloud                      2       https://registry.npmjs.org/
@cloudnative/health-connect 2       https://registry.npmjs.org/@***.tgz
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • Thanks alot guys. I added the "name" into the call and worked as needed. @{ Name = 'Name' Expression = { $_.name } }, @{ Name = 'Version' Expression = { $_.Value.Version } }, @{ Name = 'Resolved' Expression = { $_.Value.Resolved } } Thank you for your help – Caleb Adepoju Jan 20 '22 at 07:48
  • Get the error ConvertFrom-Json : Cannot process argument because the value of argument "name" is not valid. Change the value of the "name" argument and run the operation again. – Caleb Adepoju Jan 20 '22 at 08:33