Convert JSON to csv in Powershell

Hi,

Hope someone could help me with this one (I am not a Developer).
I need to convert a local JSON file (see attached file) to a csv. in Powershell

In this way I can create an SSIS that will read the file and the column I need.

The extension  should be JSON instead of txt.


I got to a point that a file been created but without records

$pathToOutputFile = "C:\Freshdesk\Daily_Export_json.csv"
$file = "C:\Freshdesk\Daily_Export.json"
Get-Content -Path $file
$json = ConvertFrom-JSON (Get-Content $file -Raw)
$json.DataFeed.Rows | Select "performed_at",
"ticket_id","performer_type","activity/automation/type","activity/automation/rule","activity/send_email/agent_ids/0","performer_id","activity/note/id",
"activity/note/type","activity/status","activity/ticket_type","activity/requester_id","activity/source","activity/priority","activity/new_ticket","activity/Product Fixlist",
"activity/agent_id","activity/deleted,activity/Choose Product","activity/Choose Component","activity/How can we help?","activity/Product","activity/Product version","activity/group",
"activity/Vendor Ticket#","activity/description","activity/send_email/requester_id/0" | Export-CSV $pathToOutputFile

Open in new window



Thanks,
Roey
Daily_Export.txt
Roey MAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Juan OcasioApplication DeveloperCommented:
Roey MAuthor Commented:
Hi,

Thanks. We already look into this article... We got stuck in some points.

This is why I need a script that will take the JSON file and convert it to a CSV file, this is a much simple method.
oBdACommented:
Test this thoroughly; it seems to have worked with your sample, but I have no idea how it will fare with other data.
For initial testing, it might be easier to replace the "Export-Csv ..." in the last line with "Out-GridView".
$jsonFile = "C:\Freshdesk\Daily_Export.json"
$outputFile = "C:\Freshdesk\Daily_Export_json.csv"

$json = Get-Content -Path $jsonFile | ConvertFrom-Json
$properties = @(
	'performed_at'
	'ticket_id'
	'performer_type'
	'activity/automation/type'
	'activity/automation/rule'
	'activity/send_email/agent_ids/0'
	'performer_id'
	'activity/note/id'
	'activity/note/type'
	'activity/status'
	'activity/ticket_type'
	'activity/requester_id'
	'activity/source'
	'activity/priority'
	'activity/new_ticket'
	'activity/Product Fixlist'
	'activity/agent_id'
	'activity/deleted'
	'activity/Choose Product'
	'activity/Choose Component'
	'activity/How can we help?'
	'activity/Product'
	'activity/Product version'
	'activity/group'
	'activity/Vendor Ticket#'
	'activity/description'
	'activity/send_email/requester_id/0'
)
$json.activities_data | ForEach-Object {
	$row = '' | Select-Object -Property $properties
	ForEach ($property In $properties) {
		$Value = $_.psobject.Copy()
		ForEach ($subProperty in $property.Split('/')) {
			If (($Value -is [array]) -and (($subProperty -as [Int32]) -ne $null)) {
				$Value = $Value[[Int32]$subProperty]
			} Else {
				$Value = $Value.$subProperty
				If ($Value -eq $null) {break}
			}
		}
		$row.$property = $Value
	}
	$row
} | Export-Csv -NoTypeInformation -Path $outputFile

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Check Out How Miercom Evaluates Wi-Fi Security!

It's not just about Wi-Fi connectivity anymore. A wireless security breach can cost your business large amounts of time, trouble, and expense. Plus, hear first-hand from Miercom on how WatchGuard's Wi-Fi security stacks up against the competition plus a LIVE demo!

leakim971PluritechnicianCommented:
That worked for me :
$pathToOutputFile = "C:\Freshdesk\Daily_Export_json.csv"
$file = "C:\Freshdesk\Daily_Export.json"
Get-Content -Path $file
$json = ConvertFrom-JSON (Get-Content $file -Raw)
$json.activities_data | Select "performed_at","ticket_id","performer_type",@{Label = "type"; Expression = { $_.activity.automation.type }},@{Label = "rule"; Expression = { $_.activity.automation.rule }} | Export-CSV $pathToOutputFile

Open in new window

Roey MAuthor Commented:
Hi,

Thank!
Just added the $outputFile at the end of the script.
Roey MAuthor Commented:
Hi,

Can someone adjust the script so the convert will  without double quotes(" ")??

"performed_at","ticket_id","performer_type","activity/automation/type",
oBdACommented:
Replace line 34 and below with the following.
Be aware that several of the values look like they contain more or less free text. If one of those happens to contains a comma, the resulting csv file will be invalid or, worse, the columns will shift.
Any decent csv import function/software should be able to handle quoted cells.
($json.activities_data | ForEach-Object {
	$row = '' | Select-Object -Property $properties
	ForEach ($property In $properties) {
		$Value = $_.psobject.Copy()
		ForEach ($subProperty in $property.Split('/')) {
			If (($Value -is [array]) -and (($subProperty -as [Int32]) -ne $null)) {
				$Value = $Value[[Int32]$subProperty]
			} Else {
				$Value = $Value.$subProperty
				If ($Value -eq $null) {break}
			}
		}
		$row.$property = $Value
	}
	$row
} | ConvertTo-Csv -NoTypeInformation) -replace '(",")|(",)|(,")', ',' -replace '(\A")|("\Z)' | Set-Content -Path $outputFile

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.