# Convert JSON ROWS In CSV as columns into another CSV file

**URL:** <https://forums.powershell.org/t/convert-json-rows-in-csv-as-columns-into-another-csv-file/13412>\
**Category:** PowerShell Help\
**Created:** [November 13, 2019, 8:08am UTC](https://forums.powershell.org/t/convert-json-rows-in-csv-as-columns-into-another-csv-file/13412 "2019-11-13T08:08:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kathraji](https://avatars.discourse-cdn.com/v4/letter/k/dc4da7/32.png) [@kathraji](https://forums.powershell.org/u/kathraji)\
**Post date:** [November 13, 2019, 8:08am UTC](https://forums.powershell.org/t/convert-json-rows-in-csv-as-columns-into-another-csv-file/13412/1 "2019-11-13T08:08:12Z")

</div>

My sample data looks i

```
{"Id":"value","RecordType":value,"CreationTime":"value","Operation":"value"}

```

```
{"Id":"value","RecordType":value,"CreationTime":"value","Operation":"value"}
```

```
{"Id":"value","RecordType":value,"CreationTime":"value","Operation":"value"}
```

```
I need the same data in another CSV file as below
```

```
id RecordType CreationTime Operation
value value value value
value value value value

I tried Convert-FROM-JSON function by passing the CSV as below but its failing with error
```

```
$properties = @('Id', 'RecordType', 'CreationTime', 'Operation')
(Get-Content -Path-to_CSVfile -Raw | ConvertFrom-Json) |
    Select-Object -Property $properties |
    Export-Csv -NoTypeInformation -Path $path-to-new-csv-file
```

```
ConvertFrom-Json : Invalid JSON primitive: "id"

Please help if anyone has suggestion regarding the same

Thanks,
Anil
```

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [November 13, 2019, 10:34am UTC](https://forums.powershell.org/t/convert-json-rows-in-csv-as-columns-into-another-csv-file/13412/2 "2019-11-13T10:34:09Z")

</div>

Parse each row with the appropriate column. Note that the JSON example you posted, that the RecordType is invalid. Assuming that real data is numeric, the ConvertFrom-JSON would work, but if it is a string value, then it is malformed JSON which is causing the error you mentioned. Guessing that RecordType is not a string, you could do something like so:

```
$csv = @()
$csv += [pscustomobject]@{
    Column1 = 'Foo'
    Column2 = 'Faa'
    Column3 = '{"Id":"value","RecordType":1,"CreationTime":"value","Operation":"value"}'
}
$csv += [pscustomobject]@{
    Column1 = 'Foo'
    Column2 = 'Faa'
    Column3 = '{"Id":"value2","RecordType":2,"CreationTime":"value2","Operation":"value2"}'
}

$results = foreach ($row in $csv) {
    $row.Column3 | ConvertFrom-Json
}

$results
```

Output:

```
PS C:\Users\rasim> $csv

Column1 Column2 Column3                                                                    
------- ------- -------                                                                    
Foo Faa {"Id":"value","RecordType":1,"CreationTime":"value","Operation":"value"}   
Foo Faa {"Id":"value2","RecordType":2,"CreationTime":"value2","Operation":"value2"}

PS C:\Users\rasim> $results

Id RecordType CreationTime Operation
-- ---------- ------------ ---------
value 1 value value    
value2 2 value2 value2
```

---

<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:31pm UTC](https://forums.powershell.org/t/convert-json-rows-in-csv-as-columns-into-another-csv-file/13412/3 "2024-05-16T20:31:09Z")

</div>


