# JSON to CSV, formatting problem

**URL:** <https://forums.powershell.org/t/json-to-csv-formatting-problem/23475>\
**Category:** PowerShell Help\
**Created:** [January 24, 2024, 8:58pm UTC](https://forums.powershell.org/t/json-to-csv-formatting-problem/23475 "2024-01-24T20:58:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![steckj](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@steckj](https://forums.powershell.org/u/steckj)\
**Post date:** [January 24, 2024, 8:58pm UTC](https://forums.powershell.org/t/json-to-csv-formatting-problem/23475/1 "2024-01-24T20:58:58Z")

</div>

I am converting some JSONs into CSV and its works pretty well. The script I am using is pretty simple:

```auto
get-content $json | convertfrom-json | export-csv $filepath

```

My problem is that some of the values in the JSON are contained within { }. And those values appear as `System.Object[]` is the CSV. Otherwise it works great. Any recommendations? I am pretty new to powershell so I hope its an easy one!

Edit:

Raw JSON data

```auto
 "domains": ["madeup.com"]  

```

Data After Convertfrom-JSON

```auto
domains : {madeup.com}

```

I did not realize that the raw JSON did not have { }. It looks like it adds the curly brackets after converting it.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [January 25, 2024, 6:20am UTC](https://forums.powershell.org/t/json-to-csv-formatting-problem/23475/2 "2024-01-25T06:20:12Z")

</div>

Welcome back. Good news, it is quite easy. However, you have to decide how to format the data. What do you want to end up with. I tend to concatenate the values with a semicolon. It should be different than your csv delimiter, which is a comma by default. Do you have some sample data you can add to your post to make it easier for us to help you? You can edit your original post. While you’re doing that, be sure to format your code and the sample JSON data with the preformatted option. It’s the \</\> symbol. If you don’t see it, check the settings wheel - it will be there.

---

<div class="post-metadata">

**Author:** ![steckj](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@steckj](https://forums.powershell.org/u/steckj)\
**Post date:** [January 25, 2024, 4:44pm UTC](https://forums.powershell.org/t/json-to-csv-formatting-problem/23475/3 "2024-01-25T16:44:33Z")

</div>

I edited my original post and hopefully got the information you were asking for.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [January 26, 2024, 4:19am UTC](https://forums.powershell.org/t/json-to-csv-formatting-problem/23475/4 "2024-01-26T04:19:52Z")

</div>

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

```auto
$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

```auto
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

```auto
$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

```auto
$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.

```auto
$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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 12, 2024, 11:07pm UTC](https://forums.powershell.org/t/json-to-csv-formatting-problem/23475/5 "2024-05-12T23:07:54Z")

</div>


