-
Notifications
You must be signed in to change notification settings - Fork 0
🚀[Feature]: Add Import-Json function
#7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2393766
368a35c
76f4ce6
1ef4e72
3e41fbf
8748667
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,5 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # JSON Module Examples | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # This file contains practical examples of using the Format-Json function | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # This file contains practical examples of using the Format-Json and Import-Json functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Import the module (if not already loaded) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Import-Module Json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -146,4 +146,81 @@ $formatted | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endregion | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endregion | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #region Import-Json Examples | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Example 9: Import JSON from a single file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'Example 9: Import JSON from a single file' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # First, create a sample JSON file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $configData = @{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| database = @{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| host = 'localhost' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| port = 5432 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name = 'myapp' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ssl = $true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logging = @{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| level = 'info' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| file = '/var/log/app.log' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| features = @{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| caching = $true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| analytics = $false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $configFile = '/tmp/config.json' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $configData | ConvertTo-Json -Depth 3 | Set-Content -Path $configFile | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Import the JSON file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $importedConfig = Import-Json -Path $configFile | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $importedConfig | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Database host: $($importedConfig.database.host)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Source file: $($importedConfig._SourceFile)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Example 10: Import multiple JSON files using wildcards | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'Example 10: Import multiple JSON files using wildcards' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Create multiple JSON files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $userData = @{ name = 'Alice'; role = 'admin'; active = $true } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $settingsData = @{ theme = 'dark'; notifications = $true; language = 'en' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $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' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+187
to
+194
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $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') |
Copilot
AI
Jul 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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') |
Copilot
AI
Jul 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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') |
Copilot
AI
Jul 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,94 @@ | ||||||||||
| function Import-Json { | ||||||||||
| <# | ||||||||||
| .SYNOPSIS | ||||||||||
| Imports JSON data from a file. | ||||||||||
|
|
||||||||||
| .DESCRIPTION | ||||||||||
| Reads JSON content from one or more files and converts it to PowerShell objects. | ||||||||||
| Supports pipeline input for processing multiple files. | ||||||||||
|
|
||||||||||
| .EXAMPLE | ||||||||||
| Import-Json -Path 'config.json' | ||||||||||
|
|
||||||||||
| Imports JSON data from config.json file. | ||||||||||
|
|
||||||||||
| .EXAMPLE | ||||||||||
| Import-Json -Path 'data/*.json' | ||||||||||
|
|
||||||||||
| Imports JSON data from all .json files in the data directory. | ||||||||||
|
|
||||||||||
| .EXAMPLE | ||||||||||
| 'settings.json', 'users.json' | Import-Json | ||||||||||
|
|
||||||||||
| Imports JSON data from multiple files via pipeline. | ||||||||||
|
|
||||||||||
| .EXAMPLE | ||||||||||
| Import-Json -Path 'complex.json' -Depth 50 | ||||||||||
|
|
||||||||||
| Imports JSON data with a custom maximum depth of 50 levels. | ||||||||||
|
|
||||||||||
| .LINK | ||||||||||
| https://psmodule.io/Json/Functions/Import-Json/ | ||||||||||
| #> | ||||||||||
|
|
||||||||||
| [CmdletBinding()] | ||||||||||
| param ( | ||||||||||
| # The path to the JSON file to import. Supports wildcards and multiple paths. Can be provided via pipeline. | ||||||||||
| [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] | ||||||||||
| [Alias('FullName')] | ||||||||||
| [string[]]$Path, | ||||||||||
|
|
||||||||||
| # The maximum depth to expand nested objects. Uses ConvertFrom-Json default if not specified. | ||||||||||
| [Parameter()] | ||||||||||
| [int]$Depth | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| process { | ||||||||||
| foreach ($filePath in $Path) { | ||||||||||
| try { | ||||||||||
| # Resolve wildcards and relative paths | ||||||||||
| $resolvedPaths = Resolve-Path -Path $filePath -ErrorAction Stop | ||||||||||
|
|
||||||||||
| foreach ($resolvedPath in $resolvedPaths) { | ||||||||||
| Write-Verbose "Processing file: $($resolvedPath.Path)" | ||||||||||
|
|
||||||||||
| # Test if the file exists and is a file (not directory) | ||||||||||
| if (-not (Test-Path -Path $resolvedPath.Path -PathType Leaf)) { | ||||||||||
| Write-Error "File not found or is not a file: $($resolvedPath.Path)" | ||||||||||
| continue | ||||||||||
| } | ||||||||||
|
|
||||||||||
| # Read file content | ||||||||||
| $jsonContent = Get-Content -Path $resolvedPath.Path -Raw -ErrorAction Stop | ||||||||||
|
|
||||||||||
| # Check if file is empty | ||||||||||
| if ([string]::IsNullOrWhiteSpace($jsonContent)) { | ||||||||||
| Write-Warning "File is empty or contains only whitespace: $($resolvedPath.Path)" | ||||||||||
| continue | ||||||||||
| } | ||||||||||
|
|
||||||||||
| # Convert JSON to PowerShell object | ||||||||||
| if ($PSBoundParameters.ContainsKey('Depth')) { | ||||||||||
| $jsonObject = $jsonContent | ConvertFrom-Json -Depth $Depth -ErrorAction Stop | ||||||||||
| } else { | ||||||||||
| $jsonObject = $jsonContent | ConvertFrom-Json -ErrorAction Stop | ||||||||||
| } | ||||||||||
|
|
||||||||||
| # 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 | ||||||||||
|
||||||||||
| 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 } |
Copilot
AI
Jul 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.