0

I have to write json response from a Rest API call to a csv for further processing. I have almost no experience writing Powershell. API response

 {
  "id": "9dea9e9f-f802-4e39-8a75-ad84fabde69f",
  "title": "Power BI Capacity \"Enterprise Apps - P1\" Compute",
  "artifactId": "3f517b9b-ae25-4247-8df4-428dfa9a759b",
  "artifactDisplayName": "Fabric Capacity Metrics",
  "subArtifactDisplayName": "Compute",
  "artifactType": "Report",
  "isEnabled": true,
  "frequency": "Weekly",
  "startDate": "2/14/2024 12:00:00 AM",
  "endDate": "2/15/20254:33:43 PM",
  "linkToContent": true,
  "previewImage": true,
  "attachmentFormat": "PDF",
  "owner": {
    "emailAddress": "[email protected]",
    "displayName": "Tito Perez",
    "identifier": "[email protected]",
    "graphId": "d631e63d-b77d-4410-a553-712dc93f1655",
    "principalType": "User"
  },
  "users": []
}

This is the code I wrote

$URL = 'https://api.powerbi.com/v1.0/myorg/admin/users/63d0139e-691f-4005-a185- 
9bde3362080f/subscriptions'
$r=Invoke-PowerBIRestMethod -URL $URL -Method Get
$JSONData=$r|ConvertFrom-Json 
$JSONData | ForEach-Object { $_.SubscriptionEntities } | Select 
id,title,artifactId,artifactDisplayName,subArtifactDisplayName,artifactType,isEnabled,
frequency,startDate,endDate,linkToContent,attachmentFormat,owner | Format-Table
$JSONData | ForEach-Object { $_.SubscriptionEntities.owner } | Select emailAddress| 
Format-Table

I want to output this to csv. I can get all entities including owner which returns as one single column. I want to parse the owner column too. The last line of code gets me owner entities like emailAddress but I want it combined in one output

0

1 Answer 1

0

If I understand what you want correctly, you can use calculated properties to select the email from the owner field like so:

$JSONData.subscriptionEntities | select id,title,artifactId,artifactDisplayName,subArtifactDisplayName,artifactType,isEnabled,frequency,startDate,endDate,linkToContent,attachmentFormat,@{Name='ownerEmail'; Expression={$_.owner.emailAddress}} | Export-Csv -Path "subscriptionEntities.csv" -NoTypeInformation
#or
$JSONData.subscriptionEntities | select *,@{Name='ownerEmail'; Expression={$_.owner.emailAddress}} | select -ExcludeProperty users,owner | Export-Csv -Path "subscriptionEntities.csv" -NoTypeInformation
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this was very helpful. It worked and I learnt a very useful technique!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.