Skip to content

🚀 Add Export-Json function for exporting JSON data to files#8

Merged
Marius Storhaug (MariusStorhaug) merged 16 commits intomainfrom
copilot/fix-3
Jul 30, 2025
Merged

🚀 Add Export-Json function for exporting JSON data to files#8
Marius Storhaug (MariusStorhaug) merged 16 commits intomainfrom
copilot/fix-3

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jul 26, 2025

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

# 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:

# 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.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Copilot AI changed the title [WIP] 🚀[Feature]: Export-Json 🚀 Add Export-Json function for exporting JSON data to files Jul 26, 2025
Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
@MariusStorhaug
Copy link
Copy Markdown
Member

Error: [-] Should use '$null = ...' instead of '... | Out-Null' (ID: OutNull) 25ms (24ms|1ms)
Message
Expected $null or empty, because the script should use '$null = ...' instead of '... | Out-Null', but got '

  • functions/public/Export-Json.ps1:L119 - New-Item -Path $directory -ItemType Directory -Force | Out-Null'.
    at Should -BeNullOrEmpty -Because "the script should use '`$null = ...' instead of '... | Out-Null'", /Users/runner/work/_actions/PSModule/Test-PSModule/v3/scripts/tests/SourceCode/PSModule/PSModule.Tests.ps1:165
    at , /Users/runner/work/_actions/PSModule/Test-PSModule/v3/scripts/tests/SourceCode/PSModule/PSModule.Tests.ps1:164

@MariusStorhaug
Copy link
Copy Markdown
Member

RuleName Severity ScriptName Line Message


PSAvoidTrailingWhitespace Information Json.Tests 697 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 702 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 705 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 716 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 718 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 724 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 726 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 737 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 739 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 750 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 755 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 764 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 769 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 777 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 779 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 788 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 790 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 792 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 796 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 799 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 803 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 810 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 814 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 818 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 821 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 834 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 836 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 853 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 856 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 870 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 877 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 881 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 894 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 896 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 899 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 905 Line has trai
.ps1 ling whitespa
ce
PSAvoidTrailingWhitespace Information Json.Tests 908 Line has trai
.ps1 ling whitespa
ce

RuleName Severity ScriptName Line Message


PSAlignAssignmentStatement Warning General.ps 231 Assignment st
1 atements are
not aligned
PSAlignAssignmentStatement Warning General.ps 232 Assignment st
1 atements are
not aligned
PSAlignAssignmentStatement Warning General.ps 233 Assignment st
1 atements are
not aligned
PSAlignAssignmentStatement Warning General.ps 234 Assignment st
1 atements are
not aligned
PSAlignAssignmentStatement Warning General.ps 235 Assignment st
1 atements are
not aligned

RuleName Severity ScriptName Line Message


PSPlaceCloseBrace Warning Export-Jso 146 Close brace b
n.ps1 efore a branc
h statement i
s followed by
a new line.
PSPlaceCloseBrace Warning Export-Jso 149 Close brace b
n.ps1 efore a branc
h statement i
s followed by
a new line.
PSPlaceCloseBrace Warning Export-Jso 152 Close brace b
n.ps1 efore a branc
h statement i
s followed by
a new line.
PSPlaceCloseBrace Warning Export-Jso 155 Close brace b
n.ps1 efore a branc
h statement i
s followed by

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jul 28, 2025

Error: [-] Should use '$null = ...' instead of '... | Out-Null' (ID: OutNull) 25ms (24ms|1ms)
Message
Expected $null or empty, because the script should use '$null = ...' instead of '... | Out-Nul...

Fixed the linting issue by replacing | Out-Null with $null = ... pattern on line 119. The functionality remains unchanged and directory creation works correctly. (1f501f1)

…d fix brace placement

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jul 28, 2025

RuleName Severity ScriptName Line Message


PSAvoidTrailingWhitespace Informat...

Fixed all linting issues:

  • Removed trailing whitespace from 37 lines in Json.Tests.ps1
  • Aligned assignment statements in General.ps1 examples
  • Fixed close brace placement in Export-Json.ps1 catch blocks

All PowerShell files now pass syntax validation and follow proper formatting standards. (9d0633d)

@MariusStorhaug Marius Storhaug (MariusStorhaug) marked this pull request as ready for review July 28, 2025 08:14
Copilot AI review requested due to automatic review settings July 28, 2025 08:14
Copilot AI and others added 2 commits July 28, 2025 10:55
…bility

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
…e upfront

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Copilot AI and others added 2 commits July 28, 2025 14:17
…adability

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
@MariusStorhaug
Copy link
Copy Markdown
Member

Correct the tests for Format-Json to expect indents x 2 spaces by default, instead of 4.

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jul 29, 2025

Correct the tests for Format-Json to expect indents x 2 spaces by default, instead of 4.

Updated the Format-Json test data to use 2-space indentation instead of 4-space to match the new default IndentationSize. The $prettyJson test data in the BeforeAll section now correctly uses 2-space indentation, ensuring consistency with the updated default parameter value. All tests are now passing. (0272237)

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
@MariusStorhaug Marius Storhaug (MariusStorhaug) merged commit f2e2c21 into main Jul 30, 2025
59 checks passed
@MariusStorhaug Marius Storhaug (MariusStorhaug) deleted the copilot/fix-3 branch July 30, 2025 10:24
@github-actions
Copy link
Copy Markdown

Module Json - 1.2.0 published to the PowerShell Gallery.

@github-actions
Copy link
Copy Markdown

GitHub release for Json v1.2.0 has been created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🚀[Feature]: Export-Json

3 participants