# ConvertFrom-Json to CSV

**URL:** <https://forums.powershell.org/t/convertfrom-json-to-csv/8837>\
**Category:** PowerShell Help\
**Created:** [June 15, 2017, 8:40am UTC](https://forums.powershell.org/t/convertfrom-json-to-csv/8837 "2017-06-15T08:40:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![twoolums](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@twoolums](https://forums.powershell.org/u/twoolums)\
**Post date:** [June 15, 2017, 8:40am UTC](https://forums.powershell.org/t/convertfrom-json-to-csv/8837/1 "2017-06-15T08:40:45Z")

</div>

Hello having an issue trying to convert a json file to csv. Any help would be appreciated.

Thanks in advance

I have a json file in the following format:

```
{
        "public_cluster": {
                "columns": ["ID", "NAME", "CLUSTER_NAME", "V_NAME", "DATACENTER_NAME", "DATE_PULL", "TIME_PULL"],
                "records": [
                        ["ID12345", "cluser1", "pretty_cluster", "server1", "city123", "06\/14\/2017", "15:34:15"],
                        ["ID12345678", "cluster2", "cluser_big", "server4", "cit3", "06\/14\/2017", "12:58:32"]
                ]
        }
}
```

I’ve tried various things like

(get-content -path c:\temp\file.json -Raw | convertfrom-json) | convertto-csv -notypeinformation | select-object | set-content c:\temp\out.csv

also tried looking at the rows  
$json = convertfrom-json (get-content c:\temp\file.json -raw)  
$json.DataFeed.Rows | select \*

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [June 15, 2017, 8:46am UTC](https://forums.powershell.org/t/convertfrom-json-to-csv/8837/2 "2017-06-15T08:46:54Z")

</div>

It would help to understand what the “issue” is for you.

In your first example, I don’t understand why you’re using Select-Object. It would appear to be selecting nothing. I’m perhaps unclear why you didn’t just use Export-CSV? “Get-Content | ConvertFrom-JSON | Export-CSV” ?

In your second example, I don’t understand why DataFeed is used. I don’t see that element in your JSON at all.

But more broadly, I’m not sure you can do what I think you’re trying to do. Each line of a CSV is a single object, and its properties are the CSV columns. Your JSON, however, isn’t lending itself to that. You’re creating one object with a public\_cluster property, which has a columns property, which is an array of values. This JSON is not going to be interpreted in a way that can be converted to a CSV with those columns values as the CSV column names. There isn’t even any straightforward way to rejigger than JSON into something that looks like a CSV. You’d basically have to ingest the JSON and then do a ton of manual programming to format the data into the individual, row-by-row objects that would be able to be a CSV.

---

<div class="post-metadata">

**Author:** ![twoolums](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@twoolums](https://forums.powershell.org/u/twoolums)\
**Post date:** [June 15, 2017, 8:55am UTC](https://forums.powershell.org/t/convertfrom-json-to-csv/8837/3 "2017-06-15T08:55:05Z")

</div>

You can probably guess, but I’m not getting output I would expect. Just would see System.Object in the outfile. I’m essentially just trying to turn the json into a flat file, record by record so it could be imported elsewhere. High level I just want to get the records area of the file into a csv.

“ID12345”, “cluser1”, “pretty\_cluster”, “server1”, “city123”, “06/14/2017”, “15:34:15”  
“ID12345678”, “cluster2”, “cluser\_big”, “server4”, “cit3”, “06/14/2017”, “12:58:32”

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [June 15, 2017, 8:58am UTC](https://forums.powershell.org/t/convertfrom-json-to-csv/8837/4 "2017-06-15T08:58:29Z")

</div>

Your JSON is producing a single object; because it’s a generic object, its text rendering is just “System.Object,” which is why you get that in your file.

There’s no easy conversion here due to the way the JSON is formatted. You’re going to need to write code to enumerate the columns, enumerate each row, and produce an output object for each row.

```
[pscustomobject]@{'Column1'='Value1';'Column2'='Value2'}
```

That’s essentially what you need to output for each row of the CSV. Send all that to Export-CSV and it’ll do what you want. But there’s no simple command to go from your JSON to that state - it’ll be manually coding it up.

---

<div class="post-metadata">

**Author:** ![dirk](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dirk/32/475_2.png) [@dirk](https://forums.powershell.org/u/dirk)\
**Post date:** [June 15, 2017, 9:26am UTC](https://forums.powershell.org/t/convertfrom-json-to-csv/8837/5 "2017-06-15T09:26:30Z")

</div>

Hi,  
Here is one of the ways to do what you want:

```
$json.public_cluster.records | ForEach-Object {
    $_ -join ',' | ConvertFrom-Csv -Header $output.public_cluster.Columns | 
        Export-Csv -Path C:\test.csv -NoTypeInformation
}
```

---

<div class="post-metadata">

**Author:** ![twoolums](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@twoolums](https://forums.powershell.org/u/twoolums)\
**Post date:** [June 15, 2017, 10:41am UTC](https://forums.powershell.org/t/convertfrom-json-to-csv/8837/6 "2017-06-15T10:41:23Z")

</div>

Thanks Dirk that did it, just had to add the --Append on the Export-CSv so it would do all the records vs just one.

Code that did the trick for me.

```
$file = "C:\temp\public_cluster.json"
$json = ConvertFrom-JSON (Get-Content $file -Raw)
$json.public_cluster.records | ForEach-Object {
   $_ -join ',' | ConvertFrom-CSV -Header $json.public_cluster.columns | Export-CSV -path C:\temp\public_cluster.csv -NoTypeInformation -Append
}
```

---

<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 16, 2024, 8:38pm UTC](https://forums.powershell.org/t/convertfrom-json-to-csv/8837/7 "2024-05-16T20:38:35Z")

</div>


