# Updating json file dynamically

**URL:** <https://forums.powershell.org/t/updating-json-file-dynamically/14518>\
**Category:** PowerShell Help\
**Created:** [June 10, 2020, 11:28pm UTC](https://forums.powershell.org/t/updating-json-file-dynamically/14518 "2020-06-10T23:28:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![incognitotom](https://avatars.discourse-cdn.com/v4/letter/i/e495f1/32.png) [@incognitotom](https://forums.powershell.org/u/incognitotom)\
**Post date:** [June 10, 2020, 11:28pm UTC](https://forums.powershell.org/t/updating-json-file-dynamically/14518/1 "2020-06-10T23:28:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [June 11, 2020, 4:25am UTC](https://forums.powershell.org/t/updating-json-file-dynamically/14518/2 "2020-06-11T04:25:06Z")

</div>

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"
                      }
                  ]
}
```

---

<div class="post-metadata">

**Author:** ![incognitotom](https://avatars.discourse-cdn.com/v4/letter/i/e495f1/32.png) [@incognitotom](https://forums.powershell.org/u/incognitotom)\
**Post date:** [June 11, 2020, 7:59am UTC](https://forums.powershell.org/t/updating-json-file-dynamically/14518/3 "2020-06-11T07:59:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/updating-json-file-dynamically/14518/4 "2024-05-16T20:29:24Z")

</div>


