Please test your code/samples. What you provided is not valid JSON. However, adding the appropriate opening and closing curly braces makes it valid. I’ve also expanded your example to make it clearer what we are doing
$tempfile = New-TemporaryFile
@'
{
"domains": ["madeup.com"],
"Property": "Property",
"Properties": [
"Property1",
"Property2"
]
}
'@ | Set-Content $tempfile
Ok now that I have so sample data to use, let’s break it down. The [ ]
brackets in json indicates an array of data. Your example was an array of one item. You can see the difference in the output after conversion
Get-Content $tempfile|ConvertFrom-Json
domains Property Properties
------- -------- ----------
{madeup.com} Property {Property1, Property2}
Notice the single property does indeed not have the curly braces. We could check each property to see if it’s an array. For this, we can use the the under-the-hood psobject. First we’ll get all the property names, then use those to check the values of each
$jsondata = Get-Content $tempfile | Convertfrom-Json
$propertynames = $jsondata.psobject.properties.name
foreach($property in $propertynames){
$jsondata.$property -is [array]
}
True
False
True
For each that is an array, -join them and assign back to each property
$jsondata = Get-Content $tempfile | Convertfrom-Json
$propertynames = $jsondata.psobject.properties.name
foreach($property in $propertynames){
if($jsondata.$property -is [array]){
$jsondata.$property = $jsondata.$property -join '; '
}
}
$jsondata
domains Property Properties
------- -------- ----------
madeup.com Property Property1; Property2
You can see the curly braces are gone. However, for this example we don’t need to check if it’s an array. We can just -join each property and if it’s a single value, it won’t change anything.
$jsondata = Get-Content $tempfile | Convertfrom-Json
$propertynames = $jsondata.psobject.properties.name
foreach($property in $propertynames){
$jsondata.$property = $jsondata.$property -join '; '
}
$jsondata
domains Property Properties
------- -------- ----------
madeup.com Property Property1; Property2
Now you can export $jsondata
to csv and it will be the values as seen there with the semicolon