You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
🚀 Add Export-Json function for exporting JSON data to files (#8)
This PR implements the `Export-Json` function to complete the JSON
module's core functionality, providing a comprehensive solution for JSON
file operations alongside the existing `Format-Json` and `Import-Json`
functions.
## Key Features
The `Export-Json` function provides flexible JSON export capabilities
with the following features:
- **Dual Input Support**: Accepts both PowerShell objects
(`-InputObject`) and JSON strings (`-JsonString`)
- **Flexible Formatting**: Supports all `Format-Json` formatting options
including spaces/tabs indentation, custom indentation sizes, and compact
output
- **Pipeline Processing**: Handles multiple objects via pipeline (last
object overwrites when using same path)
- **File System Management**: Automatically creates directories as
needed and handles file overwrites with `-Force` parameter
- **PowerShell Integration**: Full support for `-WhatIf`, various text
encodings, and custom serialization depth
## Usage Examples
```powershell
# Export a PowerShell object with custom formatting
$config = @{ server = 'localhost'; port = 8080; ssl = $true }
Export-Json -InputObject $config -Path 'config.json' -IndentationType Spaces -IndentationSize 2
# Export JSON string in compact format
Export-Json -JsonString '{"name":"test","value":123}' -Path 'data.json' -Compact
# Pipeline export (last object overwrites previous)
@($obj1, $obj2, $obj3) | Export-Json -Path 'output.json' -IndentationType Tabs
```
## Integration with Existing Functions
The function integrates seamlessly with existing module functions:
```powershell
# Complete roundtrip workflow
$data = Import-Json -Path 'input.json'
$formatted = Format-Json -InputObject $data -IndentationType Tabs
Export-Json -JsonString $formatted -Path 'output.json'
```
## Testing
Added comprehensive test coverage with 12 test cases covering:
- Basic object and JSON string exports
- All formatting options (spaces, tabs, compact)
- Pipeline processing behavior
- File system operations and error handling
- Integration with `Import-Json` for roundtrip validation
- Edge cases and error scenarios
All tests pass, and the implementation follows the established patterns
and conventions used by `Format-Json` and `Import-Json`.
Fixes#3.
<!-- START COPILOT CODING AGENT TIPS -->
---
💬 Share your feedback on Copilot coding agent for the chance to win a
$200 gift card! Click
[here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to
start the survey.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
0 commit comments