This simple script helps you to extract only the paths you want from a swagger json and generates a new swagger json with all schemas used in the selected paths.
- Download the SwaggerJsonExtractor.ps1
- Open a PowerShell console in the same directory as the script is stored
- Provide the script with the source swagger json, you want to extract from, in one of the following ways:
- Pipe the swagger string into the script
PS> '{"openapi": "3.0.1", "info": {"title": ""}/*[...]*/}' | ./SwaggerJsonExtractor.ps1
- Pass the swagger string as a parameter
PS> ./SwaggerJsonExtractor.ps1 -SwaggerJsonString '{"openapi": "3.0.1", "info": {"title": ""}/*[...]*/}'
- Copy the swagger string into your Clipboard and run script
PS> ./SwaggerJsonExtractor.ps1
- Pass a FilePath to the swagger json file
PS> ./SwaggerJsonExtractor.ps1 -InputPath "C:\temp\swagger.json"
- Enter the relative endpoint paths as they are shown in swagger doc
You can also pass an array directly as a parameter when calling the script
PS> PathsToExtract[0]: /api/user/{id} PS> PathsToExtract[1]: /api/group/{id}
PS> ./SwaggerJsonExtractor.ps1 -PathsToExtract @("/api/user/{id}", "/api/group/{id}")
- The script outputs the new json to the console and pastes the json to your clipboard.
You can specify a outputfile with the property-OutputPath
to have the result stored to a file You can use-ForceFileOutput
to overwrite the file specified by-OutputPath
if it already exists
You can suppress the console output and the clipboard paste by using the-SuppressConsoleOutput
and-SuppressClipboardOutput