File size: 3,095 Bytes
8bea2b9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
<#
.SYNOPSIS
Formats JSON files to a single-line format using the jq utility.
.DESCRIPTION
This script contains two functions: Format-JSONFilesToSingleLine and Format-JSONFileToSingleLine.
The Format-JSONFilesToSingleLine function accepts a path as input and processes either a single file or all JSON files within a directory to convert them to a single-line format.
The Format-JSONFileToSingleLine function formats a single JSON file to a single-line format.
.PARAMETER Path
Specifies the path to either a JSON file or a directory containing JSON files to be formatted.
.EXAMPLE
Format-JSONFilesToSingleLine -Path "C:\path\to\your\directory"
Formats all JSON files in the specified directory to a single-line format.
.EXAMPLE
Format-JSONFileToSingleLine -FilePath "C:\path\to\your\file.json"
Formats the specified JSON file to a single-line format.
.NOTES
- Requires the jq utility to be installed and added to the system PATH.
- Overwrites the original JSON files with their single-line formatted versions.
- If the jq utility is not installed, the script displays an error message and exits.
- If the specified file or directory does not exist, the script displays an error message.
Author: _ka_de
Date: 2024-04-20
Version: 1.0
#>
function Format-JSONFilesToSingleLine {
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string]$Path
)
process {
if (Test-Path $Path -PathType Container) {
# If the path is a directory, format all JSON files in the directory
Get-ChildItem -Path $Path -Filter "*.json" | ForEach-Object {
Format-JSONFileToSingleLine -FilePath $_.FullName
}
}
elseif (Test-Path $Path -PathType Leaf) {
# If the path is a single file, format that file
Format-JSONFileToSingleLine -FilePath $Path
}
else {
Write-Host "Invalid path: $Path"
}
}
}
function Format-JSONFileToSingleLine {
param (
[Parameter(Mandatory = $true)]
[string]$FilePath
)
# Check if jq is installed
if (-not (Test-Path "jq")) {
Write-Host "jq is not installed. Please install jq and ensure it's added to the system PATH."
return
}
# Check if the file exists
if (-not (Test-Path $FilePath -PathType Leaf)) {
Write-Host "File not found: $FilePath"
return
}
# Construct the command to format the JSON file to single line using jq
$command = "jq -c . '$FilePath' > '$FilePath.tmp' && move /Y '$FilePath.tmp' '$FilePath'"
# Execute the command
Invoke-Expression -Command $command
Write-Host "Formatted $($FilePath) to single-line successfully."
}
# Usage examples:
# Format a single JSON file to single-line format
# Format-JSONFileToSingleLine -FilePath "C:\path\to\your\file.json"
# Format all JSON files in a directory to single-line format
# Format-JSONFilesToSingleLine -Path "C:\path\to\your\directory" |