Created
May 5, 2019 21:01
-
-
Save jhochwald/d2915366d233e43056a6b6a6d5623635 to your computer and use it in GitHub Desktop.
PowerShell: Covert YAML to JSON and JSON to YAML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a YAML Document (Source https://yaml.org/start.html) | |
$RawYaml = @' | |
--- | |
invoice: 34843 | |
date : 2001-01-23 | |
bill-to: &id001 | |
given : Chris | |
family : Dumars | |
address: | |
lines: | | |
458 Walkman Dr. | |
Suite #292 | |
city : Royal Oak | |
state : MI | |
postal : 48046 | |
ship-to: *id001 | |
product: | |
- sku : BL394D | |
quantity : 4 | |
description : Basketball | |
price : 450.00 | |
- sku : BL4438H | |
quantity : 1 | |
description : Super Hoop | |
price : 2392.00 | |
tax : 251.42 | |
total: 4443.52 | |
comments: > | |
Late afternoon is best. | |
Backup contact is Nancy | |
Billsmer @ 338-4338. | |
'@ | |
# Convert YAML to PowerShell Object | |
$PsYaml = (ConvertFrom-Yaml -Yaml $RawYaml) | |
# Convert the Object to JSON | |
$PsJson = @($PsYaml | ConvertTo-Json) | |
# Convert JSON back to PowerShell Array | |
$PsArray = @($PsJson | ConvertFrom-Json) | |
# Convert the Array to YAML | |
ConvertTo-Yaml -Data $PsArray |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment