Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using Powershell.

Problem:

Data coming from JSON API request & Stored in Variable "$Ticket".
When i export it to CSV using Export-CSV, data not populating in Column format instead coming in Row format for Custom_field & Values.

Code Result Coming in Row Format:
=====================================
URL Custom_field Values

https://<url>.com/api/31325.json 45504409, 360000890277, GSL,False,False
,27190369, 38670589,



Expected Result Should be in Columns for (Custom_Field & Values ) to be Exported as CSV.
========================================================================================

URL Custom_field Values

https://<url>/api/v2/tickets/31325.json 45504409 GSL
https://<url>/api/v2/tickets/24422.json 360000890277 False

What I have tried:

<===========Code Start===========>

$outputfiles = 'C:\outputfile2.csv'

$tickets | Select-Object URL,  @{Name="custom_fields";Expression={$_.custom_fields.id -join ", "}}, @{Name="Values";Expression={$_.custom_fields.value -join ", "}}` | Export-Csv -Path $outputfiles -NoTypeInformation

<=============Code End=============>
Posted
Comments
Richard MacCutchan 21-Apr-21 10:08am    
What is the content of variable $tickets ?
varunchitra 21-Apr-21 10:12am    
Sending contents of $Tickets

https://drive.google.com/folderview?id=1m6GoNgmgqFvOVzr4Lz3-ms2GoZpx2A1d
Richard MacCutchan 21-Apr-21 10:52am    
Now I get:
"url","custom_fields","Values"
"https://gracesolutions.zendesk.com/api/v2/tickets/31214.json","45504409, 360000890277, 38797525, 45532589, 27190369, 38670589, 27121449, 32184825, 360000047178, 360000049497, 360013303137, 360013303157, 360000047198, 360000047218, 360000047238, 360000047258, 360001139477, 360003405517, 360003398357, 360003378158, 360015502337, 360015502357, 360015502377, 360015502397, 360015503677, 360015502417, 360015565417, 360015507418, 360016229598, 360015502317",", , , , , , triage, , False, False, False, False, 0.000, 0.000, 0.500, 0.000, , , , , , , , , , , , , , "

Richard MacCutchan 21-Apr-21 11:01am    
I think the issue is that the JSON data is a single element, so when converted it is just one object. So converting a single object to CSV creates just one row.
varunchitra 21-Apr-21 12:13pm    
I'm sending my full code and output CSV getting from this code.

Please check how to get Custom_fields in each column

Link. https://drive.google.com/folderview?id=1m6GoNgmgqFvOVzr4Lz3-ms2GoZpx2A1d

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900