I have a Array JSON File and i want to set variables corresponding to each key value pair in the JSON
Sample JSON
{
"rel": "true",
"test": "true",
"artifact": "protfolio",
"build": [
{
"appkey": "azure",
"build_language": "dotnet",
"script": "",
"file": "",
"test_script": ""
},
{
"appkey": "npm",
"build_language": "dotnet",
"script": "",
"file": "",
"test_script": ""
}
]
}
Now i want to set the variables as below iterating through the JSON
Name Value
$rel true
$test true
$artifact portfolio
$build_appkey_azure azure
$build_buildlanguage_azure dotnet
$build_script_azure
$build_file_azure
$build_testscript_azure
$build_appkey_npm npm
$build_buildlanguage_npm dotnet
$build_script_npm
$build_file_npm
$build_testscript_npm
I am able to set the non array elements but not sure how to set the array variables in the way i want, any help would be appreciated
$json_path = Get-ChildItem "test.json" -recurse | %{$_.FullName}
$json = Get-Content $json_path -Raw | ConvertFrom-Json
$json.psobject.properties| Foreach { $hashtable[$_.Name] = $_.Value }
#echo $hashtable
$hashtable.GetEnumerator() | Sort key | ForEach-Object {
$jsonKey = $_.Key
$jsonValue = $_.Value
$Basetype = $jsonValue.GetType() | Select-Object BaseType
if ( $Basetype.BaseType.Name -eq "Array") {
$val = $hashtable.Get_Item($jsonKey)
#echo $val
echo $val.appkey
echo $val.build_language
#New-Variable -name $($val.name) -value $($val.value) -Force
}
else{
New-Variable -name $jsonKey -value $jsonValue -Force
}
}