Updating json file dynamically

Hi,

I have a script that basically downloads a swagger.json spec from an API. I then extract the methods/URIs from the swagger.json I then need to write the extracted methods back into another .json file so that I have a list of all the available paths/methods for the API for which I can then run a set of tests against.

The problem I am having is getting the extracted methods/URIs back into the JSON file in the correct format/syntax.

So the .json file is in this structure.

{
   "endpoints": [{
        "getDetails": "/getDetails"
    }]
}

So I’m trying to add the next endpoint so it will become

{
   "endpoints": [{
        "getDetails": "/getDetails",
        "newEndpointName": "/newUri"
   }]
}

The from my script I can extract the Name: Value from the swagger, by doing this

$R = Get-Content $outPath | ConvertFrom-Json
     $URI = $R.paths
     $R = @{}
       $URI.psobject.Properties | ForEach-Object {
       $R.Value=($Path)
       $R.Name=($Path).Split('/')[2]
$R | ForEach-Object -Process { $json.APIEndpoints += $_ }
The $outpath is the downloaded swagger spec. The result of this gives
Name         Value
----         -----
Value        /newUri}
Name         newEndpointName
Now I just can't get this back into the .json in the correct format.

So any help you can provide would be great.

Thanks

Not sure why Get-Content is needed but you’re only showing a portion of the code. If you use Invoke-RestMethod it will be returned automatically as a PSObject and not need for files and conversions, just save the response to a variable:

$json = @"
{
   "endpoints": [{
        "getDetails": "/getDetails"
    }]
}
"@

#Response from Invoke-RestMethod
$results = $json | ConvertFrom-Json

#Update endpoints object
$endpoints = $results.endpoints |
             Select getDetails, 
                    @{Name='newEndpointName';Expression={'/newUri'}}

#generate the endpoints object
$newJson = [pscustomobject]@{endpoints=@($endpoints)}
#new json, this can be set to $body and then POST\PUT to the API
$newjson | ConvertTo-Json

Output:

{
    "endpoints":  [
                      {
                          "getDetails":  "/getDetails",
                          "newEndpointName":  "/newUri"
                      }
                  ]
}

Thanks, Rob

The Swagger file contains around 50 URIs and its not just as simple as a single JSON element. I’m trying to extract out all the available URIs/Methods for a particular API. When I tried Invoke-RestMethod it throws an error within the swagger specification which I’m not able to resolve. So I was downloading the swagger first and then get-content | ConvertFrom-Json

This gives me an object with loads of data. I then try to filter out what I need to get and end up with:

Name    Value
----    -----
Value   /newUri}
Name    newEndpointName

for about 50 items - I’m trying to add this back into a different JSON file which contains all the paths for every API I have 25+ to create a list I can then use to create tests against.

I can get it like this back into a .json but not really ideal as I need it in “Name”: “Path” format

  "Endpoints": [
    {
      "Name": "AdHocIncome",
      "Value": "/api/AdHocIncome"
    },
    {
      "Name": "BankAccountDetails",
      "Value": "/api/BankAccountDetails/{sortCode}"
    },
    {
      "Name": "BankAccountValidation",
      "Value": "/api/BankAccountValidation/{sortCode}/{accountNumber}"
    }
  ]
I hope that makes sense, but I can't work out what you put before to work as I need?

Thanks