Skip to content

🚀[Feature]: Add Import-Json function#7

Merged
Marius Storhaug (MariusStorhaug) merged 6 commits intomainfrom
copilot/fix-4
Jul 25, 2025
Merged

🚀[Feature]: Add Import-Json function#7
Marius Storhaug (MariusStorhaug) merged 6 commits intomainfrom
copilot/fix-4

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jul 25, 2025

This pull request introduces the Import-Json function 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-Json

  • src/functions/public/Import-Json.ps1: Added the Import-Json function, 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 for Import-Json, such as importing JSON from single and multiple files, pipeline usage, error handling, and combining with Format-Json.

Documentation Improvement

  • examples/General.ps1: Updated the file header to reflect the addition of Import-Json examples.

Comprehensive Testing

  • tests/Json.Tests.ps1: Added a new test context for Import-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.

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Copilot AI changed the title [WIP] 🚀[Feature]: Import-Json 🚀 Add Import-Json function to import JSON data from files Jul 25, 2025
Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
@MariusStorhaug Marius Storhaug (MariusStorhaug) changed the title 🚀 Add Import-Json function to import JSON data from files 🚀[Feature]: Add Import-Json function Jul 25, 2025
@MariusStorhaug Marius Storhaug (MariusStorhaug) marked this pull request as ready for review July 25, 2025 20:34
Copilot AI review requested due to automatic review settings July 25, 2025 20:34
@MariusStorhaug Marius Storhaug (MariusStorhaug) merged commit 540a100 into main Jul 25, 2025
59 checks passed
@MariusStorhaug Marius Storhaug (MariusStorhaug) deleted the copilot/fix-4 branch July 25, 2025 20:34
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-Json function with support for single/multiple files, wildcards, pipeline input, custom depth, and source file tracking
  • Expanded examples to demonstrate various Import-Json use 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] {
Copy link

Copilot AI Jul 25, 2025

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.

Suggested change
} catch [System.ArgumentException] {
} catch [System.Management.Automation.RuntimeException] {

Copilot uses AI. Check for mistakes.

# 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
Copy link

Copilot AI Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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 uses AI. Check for mistakes.
analytics = $false
}
}
$configFile = '/tmp/config.json'
Copy link

Copilot AI Jul 25, 2025

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.

Suggested change
$configFile = '/tmp/config.json'
$configFile = Join-Path $env:TEMP 'config.json'

Copilot uses AI. Check for mistakes.
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'
Copy link

Copilot AI Jul 25, 2025

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-data.json' for cross-platform compatibility.

Suggested change
$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 uses AI. Check for mistakes.
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'
Copy link

Copilot AI Jul 25, 2025

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.

Suggested change
$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 uses AI. Check for mistakes.
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'
Copy link

Copilot AI Jul 25, 2025

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.

Suggested change
$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 uses AI. Check for mistakes.
# Example 12: Error handling with Import-Json
'Example 12: Error handling with Import-Json'
try {
Import-Json -Path '/tmp/nonexistent.json' -ErrorAction Stop
Copy link

Copilot AI Jul 25, 2025

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.

Suggested change
Import-Json -Path '/tmp/nonexistent.json' -ErrorAction Stop
Import-Json -Path (Join-Path $env:TEMP 'nonexistent.json') -ErrorAction Stop

Copilot uses AI. Check for mistakes.
@github-actions
Copy link
Copy Markdown

Module Json - 1.1.0 published to the PowerShell Gallery.

@github-actions
Copy link
Copy Markdown

GitHub release for Json v1.1.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]: Import-Json

3 participants