Forum Discussion

gwhiteman's avatar
gwhiteman
Frequent Visitor
1 year ago
Solved

Importing multiple JSON files int PBI (Data Source = Admin API GetDatasourcesAsAdmin)

I am using the Admin Power BI API “GetDatasourcesAsAdmin” to extract connection details for semantic models we manage. I have a list of models (by datasetId) which I loop through and export the infor...
  • MarkLaf's avatar
    MarkLaf
    1 year ago

    Here is a way to check for record vs. list as part of your parsing. As stated previously, you can simply wrap the records you find into a list and then treat all outputs the same.

     

     

    let
    
        Source = Folder.Files("<local file location with your json examples saved>"),
    
        //parse the json, if a single record, wrap in list so they are all list of records
        ParseJson = 
        List.Transform( 
            Source[Content] , //binaries are in "Content" column
            each let 
                json = Json.Document( _ ), 
                valTypeIsRecord = Type.Is( Value.Type( json ), type record ) 
            in
                if valTypeIsRecord //if it's a record
                then { json } //wrap in a list
                else json //otherwise leave as is
        ),
    
        //list of list of records --> list of records
        CombineRecords = List.Combine( ParseJson ), 
    
        //uniform transformation - Table.FromRecords is designed for list of records
        ToTable = 
        Table.FromRecords( 
            CombineRecords, 
            //provide exact types here so we don't have to later
            type table [
                datasourceType=text,
                connectionDetails=[server=text,database=text],
                datasourceId=text,
                gatewayId=text
            ] 
        ),
    
        ExpandConnectionDetails = 
        Table.ExpandRecordColumn(
            ToTable, 
            "connectionDetails", 
            {"server", "database"}, 
            {"connectionDetails.server", "connectionDetails.database"}
        )
    
    in
        ExpandConnectionDetails

     

     

    Pics of steps so you can see better what's going on: