Skip to content

Instantly share code, notes, and snippets.

@quonic
Last active January 21, 2017 16:15
Show Gist options
  • Save quonic/de8c277b8d47ad1050b29e07c4bf1277 to your computer and use it in GitHub Desktop.
Save quonic/de8c277b8d47ad1050b29e07c4bf1277 to your computer and use it in GitHub Desktop.
#Get-Content -path myCSV.csv | ConvertFrom-Csv -Delimiter ',' | ConvertTo-Json | Out-File myJSON.json
$myCSV = 'Job Code,Group1,Group2,Group3
Web-1234,HR,FileShare,VDI1
Web-1235,IT,FileShare,VDI2'
$myJSON = $myCSV | ConvertFrom-Csv -Delimiter ',' | ConvertTo-Json
# What I get
$myJSON = '
[
{
"Job Code": "Web-1234",
"Group1": "HR",
"Group2": "FileShare",
"Group3": "VDI1"
},
{
"Job Code": "Web-1235",
"Group1": "IT",
"Group2": "FileShare",
"Group3": "VDI2"
}
]'
$myDATA = $myJSON | ConvertFrom-JSON
# What is easier to work with
$data = ('
{
"Web-1234": {
"groups": [
"HR",
"FileShare",
"VDI1"
]
},
"Web-1235": {
"groups": [
"IT",
"FileShare",
"VDI2"
]
}
}' | ConvertFrom-Json)
# How the data would be accessed
$myDATA[0].'Job Code'
$myDATA[0].'group1'
$myDATA[0].'group2'
$myDATA[0].'group3'
# Returns Web-1234
# Verses:
$data.'Web-1234'.groups | ForEach-Object{$_}
# More ways to do the same thing
$data."Web-1234".groups | ForEach-Object{$_}
$Web1234 = "Web-1234"
$data.$Web1234.groups | ForEach-Object{$_}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment