JSON File Validator Tool
<# .SYNOPSIS JSON File Validator Tool .DESCRIPTION This script provides a tool to validate JSON files, check for syntax errors, and provide detailed information about the JSON structure. .NOTES File Name : JSONValidator.ps1 Author : [Your Name] Prerequisite : PowerShell V5.1 or later Version : 1.0 Date : [Current Date] .EXAMPLE .\JSONValidator.ps1 #> function Show-Menu { Clear-Host Write-Host "=== JSON File Validator Tool ===" -ForegroundColor Cyan Write-Host "1. Validate JSON File" Write-Host "2. Analyze JSON Structure" Write-Host "3. Search JSON" Write-Host "4. Compare Two JSON Files" Write-Host "5. Exit" } function Validate-JSONFile { $filePath = Read-Host "Enter the path to the JSON file" if (-not (Test-Path $filePath)) { Write-Host "File not found." -ForegroundColor Red return } try { $content = Get-Content $filePath -Raw $json = ConvertFrom-Json $content -ErrorAction Stop Write-Host "JSON is valid." -ForegroundColor Green $size = (Get-Item $filePath).Length $objectCount = ($content | Select-String -Pattern "{" -AllMatches).Matches.Count $arrayCount = ($content | Select-String -Pattern "\[" -AllMatches).Matches.Count Write-Host "File Size: $size bytes" Write-Host "Number of Objects: $objectCount" Write-Host "Number of Arrays: $arrayCount" } catch { Write-Host "Invalid JSON. Error details:" -ForegroundColor Red Write-Host $_.Exception.Message } } function Analyze-JSONStructure { $filePath = Read-Host "Enter the path to the JSON file" if (-not (Test-Path $filePath)) { Write-Host "File not found." -ForegroundColor Red return } try { $json = Get-Content $filePath -Raw | ConvertFrom-Json $structure = Get-JSONStructure $json Write-Host "JSON Structure:" -ForegroundColor Yellow $structure | ForEach-Object { Write-Host $_ } } catch { Write-Host "Error analyzing JSON structure: $_" -ForegroundColor Red } } function Get-JSONStructure($obj, $path = "", $depth = 0) { $output = @() $indent = " " * $depth if ($obj -is [System.Management.Automation.PSCustomObject]) { $output += "$indent$path {}" $obj.PSObject.Properties | ForEach-Object { $newPath = if ($path) { "$path.$($_.Name)" } else { $_.Name } $output += Get-JSONStructure $_.Value $newPath ($depth + 1) } } elseif ($obj -is [Array]) { $output += "$indent$path []" if ($obj.Count -gt 0) { $output += Get-JSONStructure $obj[0] "$path[0]" ($depth + 1) } } else { $type = if ($null -eq $obj) { "null" } else { $obj.GetType().Name } $output += "$indent$path : $type" } return $output } function Search-JSON { $filePath = Read-Host "Enter the path to the JSON file" if (-not (Test-Path $filePath)) { Write-Host "File not found." -ForegroundColor Red return } $searchKey = Read-Host "Enter the key to search for" try { $json = Get-Content $filePath -Raw | ConvertFrom-Json $results = Search-JSONRecursive $json $searchKey if ($results.Count -eq 0) { Write-Host "No results found for key: $searchKey" -ForegroundColor Yellow } else { Write-Host "Search Results:" -ForegroundColor Green $results | ForEach-Object { Write-Host "Path: $($_.Path)" Write-Host "Value: $($_.Value)" Write-Host "---" } } } catch { Write-Host "Error searching JSON: $_" -ForegroundColor Red } } function Search-JSONRecursive($obj, $searchKey, $currentPath = "") { $results = @() if ($obj -is [System.Management.Automation.PSCustomObject]) { $obj.PSObject.Properties | ForEach-Object { $newPath = if ($currentPath) { "$currentPath.$($_.Name)" } else { $_.Name } if ($_.Name -eq $searchKey) { $results += @{Path = $newPath; Value = $_.Value} } $results += Search-JSONRecursive $_.Value $searchKey $newPath } } elseif ($obj -is [Array]) { for ($i = 0; $i -lt $obj.Count; $i++) { $newPath = "${currentPath}[$i]" $results += Search-JSONRecursive $obj[$i] $searchKey $newPath } } return $results } function Compare-JSONFiles { $filePath1 = Read-Host "Enter the path to the first JSON file" $filePath2 = Read-Host "Enter the path to the second JSON file" if (-not (Test-Path $filePath1) -or -not (Test-Path $filePath2)) { Write-Host "One or both files not found." -ForegroundColor Red return } try { $json1 = Get-Content $filePath1 -Raw | ConvertFrom-Json $json2 = Get-Content $filePath2 -Raw | ConvertFrom-Json $differences = Compare-ObjectRecursive $json1 $json2 if ($differences.Count -eq 0) { Write-Host "The JSON files are identical." -ForegroundColor Green } else { Write-Host "Differences found:" -ForegroundColor Yellow $differences | ForEach-Object { Write-Host "Path: $($_.Path)" Write-Host "File 1 Value: $($_.Value1)" Write-Host "File 2 Value: $($_.Value2)" Write-Host "---" } } } catch { Write-Host "Error comparing JSON files: $_" -ForegroundColor Red } } function Compare-ObjectRecursive($obj1, $obj2, $path = "") { $differences = @() if ($obj1 -is [System.Management.Automation.PSCustomObject] -and $obj2 -is [System.Management.Automation.PSCustomObject]) { $allProperties = $obj1.PSObject.Properties.Name + $obj2.PSObject.Properties.Name | Select-Object -Unique foreach ($prop in $allProperties) { $newPath = if ($path) { "$path.$prop" } else { $prop } if ($obj1.PSObject.Properties.Name -notcontains $prop) { $differences += @{Path = $newPath; Value1 = ""; Value2 = $obj2.$prop} } elseif ($obj2.PSObject.Properties.Name -notcontains $prop) { $differences += @{Path = $newPath; Value1 = $obj1.$prop; Value2 = ""} } else { $differences += Compare-ObjectRecursive $obj1.$prop $obj2.$prop $newPath } } } elseif ($obj1 -is [Array] -and $obj2 -is [Array]) { $maxLength = [Math]::Max($obj1.Length, $obj2.Length) for ($i = 0; $i -lt $maxLength; $i++) { $newPath = "${path}[$i]" if ($i -ge $obj1.Length) { $differences += @{Path = $newPath; Value1 = ""; Value2 = $obj2[$i]} } elseif ($i -ge $obj2.Length) { $differences += @{Path = $newPath; Value1 = $obj1[$i]; Value2 = ""} } else { $differences += Compare-ObjectRecursive $obj1[$i] $obj2[$i] $newPath } } } elseif ($obj1 -ne $obj2) { $differences += @{Path = $path; Value1 = $obj1; Value2 = $obj2} } return $differences } # Main program loop do { Show-Menu $choice = Read-Host "`nEnter your choice (1-5)" switch ($choice) { "1" { Validate-JSONFile } "2" { Analyze-JSONStructure } "3" { Search-JSON } "4" { Compare-JSONFiles } "5" { Write-Host "Exiting program..." -ForegroundColor Yellow; break } default { Write-Host "Invalid choice. Please try again." -ForegroundColor Red } } if ($choice -ne "5") { Read-Host "`nPress Enter to continue..." } } while ($choice -ne "5")
This JSON File Validator Tool includes:
- A menu-driven interface for easy navigation.
- Functions to perform various JSON-related tasks:
- Validate JSON files and provide basic statistics
- Analyze and display the structure of JSON files
- Search for specific keys within JSON files
- Compare two JSON files and highlight differences
Key features:
- JSON Validation:
- Checks if the JSON is syntactically valid
- Provides file size, number of objects, and number of arrays
- JSON Structure Analysis:
- Displays a hierarchical view of the JSON structure
- Shows types of values (object, array, string, number, etc.)
- JSON Search:
- Allows searching for specific keys within the JSON
- Displays the path and value of found keys
- JSON Comparison:
- Compares two JSON files
- Highlights differences, including added, removed, or modified values
This tool is particularly useful for:
- Developers working with JSON data
- QA engineers validating JSON outputs
- Data analysts examining JSON structures
- Anyone needing to quickly validate, analyze, or compare JSON files
To use this script effectively:
- Run the script in PowerShell
- Use the menu options to select the desired function
- Provide the path to the JSON file(s) when prompted
- Review the output for validation results, structure analysis, search results, or file comparisons
This script provides a comprehensive set of tools for working with JSON files, making it easier to validate, understand, and compare JSON data without having to manually parse the files or use multiple tools.