Kazu
Kazu

Reputation: 137

How to change JSON to CSV?

I have a code like this

$username = "username"
$password = "password"

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))

$response = Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Uri https://api.bitbucket.org/2.0/teams/Ateam/members -Method Get

$new = $response | select Values

$new.values = $new.values | ConvertTo-Json -Compress

$new.values

$csvinfo = (Get-Content $new.values | ConvertFrom-Json) |
           Convertto-CSV -NoTypeInformation

$csvinfo | Export-Csv C:\Users\helloworld\Desktop\BitBucket\Ateam.team.csv -NoTypeInformation  

@$new.values,

I can see JSON file on my screen, I would like to change to CSV file, like above, I tried

 $csvinfo = (Get-Content $new.values | ConvertFrom-Json) |
            Convertto-CSV -NoTypeInformation

but it says

Get-Content : Cannot find drive. A drive with the name '[{"username"' does not
exist.
At line:14 char:13
+ $csvinfo = (Get-Content $new.values | ConvertFrom-Json) | Convertto-CSV
+             ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: ([{"username":String) [Get-Content], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetContentCommand

How can I change $new.values to CSV and export?

I found the answer

$csv = $new.values| ConvertFrom-JSON
$csv|Export-Csv C:\Users\helloworld\Desktop\BitBucket\ateam.team.csv -NoTypeInformation

Upvotes: 0

Views: 561

Answers (1)

Kazu
Kazu

Reputation: 137

add this

I was thinking too much....

$csv = $new.values| ConvertFrom-JSON
$csv|Export-Csv C:\Users\helloworld\Desktop\BitBucket\ateam.team.csv -NoTypeInformation

Upvotes: 0

Related Questions