🚀[Feature]: Add Import-Json function#7
🚀[Feature]: Add Import-Json function#7Marius Storhaug (MariusStorhaug) merged 6 commits intomainfrom
Import-Json function#7Conversation
Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Import-Json function
There was a problem hiding this comment.
Pull Request Overview
This pull request introduces the Import-Json function to the PowerShell JSON module, enabling users to import JSON data from files into PowerShell objects with comprehensive error handling and file path annotation.
Key changes include:
- Added a new
Import-Jsonfunction with support for single/multiple files, wildcards, pipeline input, custom depth, and source file tracking - Expanded examples to demonstrate various
Import-Jsonuse cases including error handling and integration with existing functions - Comprehensive test coverage for all function scenarios including edge cases and error conditions
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| src/functions/public/Import-Json.ps1 | Core implementation of the Import-Json function with file reading, JSON parsing, and error handling |
| examples/General.ps1 | Added practical examples demonstrating Import-Json usage patterns and integration scenarios |
| tests/Json.Tests.ps1 | Comprehensive test suite covering valid/invalid inputs, wildcards, pipeline usage, and error conditions |
| } | ||
| } catch [System.Management.Automation.ItemNotFoundException] { | ||
| Write-Error "Path not found: $filePath" | ||
| } catch [System.ArgumentException] { |
There was a problem hiding this comment.
The exception type System.ArgumentException is incorrect for JSON parsing errors. ConvertFrom-Json throws System.ArgumentException for parameter issues, but JSON parsing errors typically throw System.Management.Automation.RuntimeException or similar. This catch block may not handle JSON parsing errors as intended.
| } catch [System.ArgumentException] { | |
| } catch [System.Management.Automation.RuntimeException] { |
|
|
||
| # Add file path information as a note property for reference | ||
| if ($jsonObject -is [PSCustomObject]) { | ||
| Add-Member -InputObject $jsonObject -MemberType NoteProperty -Name '_SourceFile' -Value $resolvedPath.Path -Force |
There was a problem hiding this comment.
This condition will fail to add the _SourceFile property when JSON represents arrays or primitive types at the root level. For example, a JSON file containing [1,2,3] or "simple string" won't get the source file annotation. Consider handling these cases or documenting this limitation.
| Add-Member -InputObject $jsonObject -MemberType NoteProperty -Name '_SourceFile' -Value $resolvedPath.Path -Force | |
| Add-Member -InputObject $jsonObject -MemberType NoteProperty -Name '_SourceFile' -Value $resolvedPath.Path -Force | |
| } elseif ($jsonObject -is [Array] -or $jsonObject -is [string] -or $jsonObject -is [int] -or $jsonObject -is [bool]) { | |
| $jsonObject = [PSCustomObject]@{ Value = $jsonObject; _SourceFile = $resolvedPath.Path } |
| analytics = $false | ||
| } | ||
| } | ||
| $configFile = '/tmp/config.json' |
There was a problem hiding this comment.
Using hardcoded Unix-style paths like '/tmp/' will fail on Windows systems. Consider using Join-Path $env:TEMP 'config.json' or [System.IO.Path]::GetTempPath() for cross-platform compatibility.
| $configFile = '/tmp/config.json' | |
| $configFile = Join-Path $env:TEMP 'config.json' |
| $userFile = '/tmp/user-data.json' | ||
| $settingsFile = '/tmp/user-settings.json' | ||
|
|
||
| $userData | ConvertTo-Json | Set-Content -Path $userFile | ||
| $settingsData | ConvertTo-Json | Set-Content -Path $settingsFile | ||
|
|
||
| # Import all user-*.json files | ||
| $allUserData = Import-Json -Path '/tmp/user-*.json' |
There was a problem hiding this comment.
Using hardcoded Unix-style paths like '/tmp/' will fail on Windows systems. Consider using Join-Path $env:TEMP 'user-data.json' for cross-platform compatibility.
| $userFile = '/tmp/user-data.json' | |
| $settingsFile = '/tmp/user-settings.json' | |
| $userData | ConvertTo-Json | Set-Content -Path $userFile | |
| $settingsData | ConvertTo-Json | Set-Content -Path $settingsFile | |
| # Import all user-*.json files | |
| $allUserData = Import-Json -Path '/tmp/user-*.json' | |
| $userFile = Join-Path $env:TEMP 'user-data.json' | |
| $settingsFile = Join-Path $env:TEMP 'user-settings.json' | |
| $userData | ConvertTo-Json | Set-Content -Path $userFile | |
| $settingsData | ConvertTo-Json | Set-Content -Path $settingsFile | |
| # Import all user-*.json files | |
| $allUserData = Import-Json -Path (Join-Path $env:TEMP 'user-*.json') |
| $userFile = '/tmp/user-data.json' | ||
| $settingsFile = '/tmp/user-settings.json' | ||
|
|
||
| $userData | ConvertTo-Json | Set-Content -Path $userFile | ||
| $settingsData | ConvertTo-Json | Set-Content -Path $settingsFile | ||
|
|
||
| # Import all user-*.json files | ||
| $allUserData = Import-Json -Path '/tmp/user-*.json' |
There was a problem hiding this comment.
Using hardcoded Unix-style paths like '/tmp/' will fail on Windows systems. Consider using Join-Path $env:TEMP 'user-settings.json' for cross-platform compatibility.
| $userFile = '/tmp/user-data.json' | |
| $settingsFile = '/tmp/user-settings.json' | |
| $userData | ConvertTo-Json | Set-Content -Path $userFile | |
| $settingsData | ConvertTo-Json | Set-Content -Path $settingsFile | |
| # Import all user-*.json files | |
| $allUserData = Import-Json -Path '/tmp/user-*.json' | |
| $userFile = Join-Path $env:TEMP 'user-data.json' | |
| $settingsFile = Join-Path $env:TEMP 'user-settings.json' | |
| $userData | ConvertTo-Json | Set-Content -Path $userFile | |
| $settingsData | ConvertTo-Json | Set-Content -Path $settingsFile | |
| # Import all user-*.json files | |
| $allUserData = Import-Json -Path (Join-Path $env:TEMP 'user-*.json') |
| $userFile = '/tmp/user-data.json' | ||
| $settingsFile = '/tmp/user-settings.json' | ||
|
|
||
| $userData | ConvertTo-Json | Set-Content -Path $userFile | ||
| $settingsData | ConvertTo-Json | Set-Content -Path $settingsFile | ||
|
|
||
| # Import all user-*.json files | ||
| $allUserData = Import-Json -Path '/tmp/user-*.json' |
There was a problem hiding this comment.
Using hardcoded Unix-style paths like '/tmp/' will fail on Windows systems. Consider using Join-Path $env:TEMP 'user-*.json' for cross-platform compatibility.
| $userFile = '/tmp/user-data.json' | |
| $settingsFile = '/tmp/user-settings.json' | |
| $userData | ConvertTo-Json | Set-Content -Path $userFile | |
| $settingsData | ConvertTo-Json | Set-Content -Path $settingsFile | |
| # Import all user-*.json files | |
| $allUserData = Import-Json -Path '/tmp/user-*.json' | |
| $userFile = Join-Path $env:TEMP 'user-data.json' | |
| $settingsFile = Join-Path $env:TEMP 'user-settings.json' | |
| $userData | ConvertTo-Json | Set-Content -Path $userFile | |
| $settingsData | ConvertTo-Json | Set-Content -Path $settingsFile | |
| # Import all user-*.json files | |
| $allUserData = Import-Json -Path (Join-Path $env:TEMP 'user-*.json') |
| # Example 12: Error handling with Import-Json | ||
| 'Example 12: Error handling with Import-Json' | ||
| try { | ||
| Import-Json -Path '/tmp/nonexistent.json' -ErrorAction Stop |
There was a problem hiding this comment.
Using hardcoded Unix-style paths like '/tmp/' will fail on Windows systems. Consider using Join-Path $env:TEMP 'nonexistent.json' for cross-platform compatibility.
| Import-Json -Path '/tmp/nonexistent.json' -ErrorAction Stop | |
| Import-Json -Path (Join-Path $env:TEMP 'nonexistent.json') -ErrorAction Stop |
|
Module Json - 1.1.0 published to the PowerShell Gallery. |
|
GitHub release for Json v1.1.0 has been created. |
This pull request introduces the
Import-Jsonfunction to the module, enabling users to import JSON data from files into PowerShell objects. It also includes comprehensive examples and tests to demonstrate its functionality and ensure reliability.New Functionality:
Import-Jsonsrc/functions/public/Import-Json.ps1: Added theImport-Jsonfunction, which supports importing JSON data from single or multiple files, handling pipeline input, custom depth levels, and error scenarios. It also annotates imported objects with the source file path for traceability.Examples Update
examples/General.ps1: Expanded the examples to include detailed use cases forImport-Json, such as importing JSON from single and multiple files, pipeline usage, error handling, and combining withFormat-Json.Documentation Improvement
examples/General.ps1: Updated the file header to reflect the addition ofImport-Jsonexamples.Comprehensive Testing
tests/Json.Tests.ps1: Added a new test context forImport-Json, covering various scenarios such as importing valid and invalid JSON files, handling empty files, using wildcards, pipeline input, custom depth levels, and relative paths. These tests ensure robustness and reliability.