Skip to content

Instantly share code, notes, and snippets.

@mattmcnabb
Created September 20, 2018 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattmcnabb/2037f64f85e9f1b42fd2749e912f8407 to your computer and use it in GitHub Desktop.
Save mattmcnabb/2037f64f85e9f1b42fd2749e912f8407 to your computer and use it in GitHub Desktop.
Wrap Json with C# class in PowerShell
add-type -Path .\newtonsoft.json.dll
$CSharp = @"
namespace json
{
public class demo
{
public string prop1 {get; set;}
public int prop2 {get; set;}
public string[] prop3 {get; set;}
}
}
"@
Add-Type -TypeDefinition $Csharp
$json = @"
{
'Prop1': 'Value1',
'Prop2': 8,
'Prop3': [
'a string',
'another one'
],
'Prop4': 'nuffin'
}
"@
$output = [Newtonsoft.Json.JsonConvert]::DeserializeObject($json, [json.demo])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment