liveBook · Manning

JSON

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

JSON is a text-based data interchange format that represents structured values using objects, arrays, strings, numbers, Boolean values, and null.

JSON is a human-readable serialization format for storing structured automation data. It supports key-value pairs, strings, numbers, dates, Boolean values, arrays, and nested objects, making it a practical default when no specific requirement favors another format. section 6.2.2

Use JSON to separate automation data from PowerShell logic:

  • Build a PowerShell object from a defined structure, then convert it to a JSON string with ConvertTo-Json. section 6.2.2
  • Convert JSON text back into a PowerShell object with ConvertFrom-Json. section 6.2.2
  • Combine the string-based JSON cmdlets with Out-File and Get-Content for local files, or supply strings from web requests, SQL databases, or parameters. section 6.2.2
  • Store data files alongside directly related scripts and use them to drive reusable functions and script execution. section 6.4

Configuration-driven registry checks

Represent each check as an object containing a registry KeyPath, a Name, and Tests with an operator and value. Serialize the collection to JSON, save it, then reload it and let the script execute the checks from configuration rather than repeated hard-coded logic. section 6.2.2 section 6.4

Listing 6.4 Creating JSON
[System.Collections.Generic.List[PSObject]] $JsonBuilder = @()
$JsonBuilder.Add(@{                                                #1
    KeyPath =
    'HKLM:\SYSTEM\CurrentControlSet\Services\LanManServer\Parameters'
    Name    = 'EnableSecuritySignature'
    Tests   = @(
        @{operator = 'eq'; value = '1' }
    )
})
$JsonBuilder.Add(@{
    KeyPath =
    'HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Security'
    Name    = 'MaxSize'
    Tests   = @(
        @{operator = 'ge'; value = '32768' }
    )
})
$JsonBuilder.Add(@{
    KeyPath =
    'HKLM:\SYSTEM\CurrentControlSet\Services\LanManServer\Parameters'
    Name    = 'AutoDisconnect'
    Tests   = @(
        @{operator = 'in'; value = '1..15' }
    )
})
$JsonBuilder.Add(@{
    KeyPath =
    'HKLM:\SYSTEM\CurrentControlSet\Services\LanManServer\Parameters'
    Name    = 'EnableForcedLogoff'
    Tests   = @(
        @{operator = 'eq'; value = '1' }
        @{operator = 'eq'; value = '$null' }
    )
})
 
$JsonBuilder |                                                     #2
    ConvertTo-Json -Depth 3 |
    Out-File .\RegistryChecks.json -Encoding UTF8

Inspect the PowerShell construction of a JSON-ready collection containing registry paths, property names, operators, and expected values. section 6.2.2

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Explains object conversion and file handling, but stops short of native PowerShell JSON schema validation.
Examples
Uses registry configuration checks with key paths, property names, comparison operators, and expected values.
What it assumes
Assumes a defined data structure and basic PowerShell objects and cmdlets while explaining JSON conversion from those foundations.
What you can do after
Apply it via ConvertTo-Json and ConvertFrom-Json to externalize configuration and reuse automation logic.
How it connects
Connects JSON with PowerShell objects, ConvertFrom-Json, ConvertTo-Json, XML, CSV, and data-driven functions.

Also explained in

Compare how 35 books explain JSON →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.