Forum Discussion
Importing multiple JSON files int PBI (Data Source = Admin API GetDatasourcesAsAdmin)
- 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 ExpandConnectionDetailsPics of steps so you can see better what's going on:
Hi Akash,
Thanks for you feed back, I came to the same conclusion however I was not sure how to get around this neatly. I have manage to work around this using 2 different power queries filtering out errors, then merging them into a single table. It is not cleanest solutions however it does work as far as I can tell. I am also keen to understand how I can "use Power BI REST API to handle this"? I am not the best with PowerShell commands.
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: